Abstract Types / Type Parameters in Scala -


I am trying to write some scala codes that need to be done:

 < Code> class test [type] {def main {SomeFunc classOf [type] val testVal: Type = new type ()}}  

and it is failing. I clearly understand nothing about scala generic parameters. Clearly, the misconception is that in C ++, templates basically work as string replacements, so new types () will work until the default constructor is in the class being passed. However, in Scala, types of objects are of different types.

As you say, there are templates in C ++ in a nutshell, C ++ says That "test is a test for all types of test compiling." It makes it easy to add obstacles easily on the T, but they are in the bottom and it can be difficult to understand without reading your class user.

Scalp's parametric polymorphism (aka generics) do a lot of things like ML, Haskell, Java and C # in Scala, when you type "class test [T]" you are saying "all t No type of test for [T] "without any obstacle It's easy to formally argue, but it means that you should be clear about obstacles. For example, in Scala you can say that T-FU should be subtype to say "T-class test [T & lt;: Foo]".

Regarding consultants for C #, there is a way to add an obstacle to T, but unfortunately there is no scala.

There are a few ways to solve your problem in Scala. Type type but BT more verbatim

  class test [T] (built-in val factory: () => {val testVal = factory}  

}

Then you can create a group of useful types of factories in your system

  item factory {underlying def listfact [X] () = list [ X] () contained DEF setfact [x] () = set [x] () // etc} import factory. _WalT = New Test [Set [String]]  

If your library users need their factories then they can add to the objects of their factories. One advantage for this solution is that anything can be used with any factory, whether it is a non-RG constructor or not.

It is not - typewriter method uses reflection and there is a feature in Scala which is a type of type error about Java error

  class test [T] (Vital testVal = m.erasure.newInstance ()) to learn about the implied M: manifest [T]. With this version you still type  < / Pre> 

However, if no non-construct constructor is available, you get a runtime exception instead of a constant type error

  scala> New Test [Set [String]] java.lang.InstantiationException: scala.collection.immutable.Set at java.lang.Class.newInstance0 (Class.Java: 340)  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -