Unexpected Scala default values behavior -


Here the default values ​​behave differently, when clearly specified with a value, versus Is directly printed?

pre package blevins.example class SimpleWrap [T] {var t: T = _ def get = t} Object extension of app application {val swb = New SimpleWrap [Boolean] val b = swb Get println ("b:" + B) / b: wrong println ("swb.get:" + swb.get) // swb.get: null val swi = new SimpleWrap [int] val I = swi.get println ( "I:" + i) // i: 0 println ("swi.get:" + swi.get) // swi.get: null}

I use 2.8r19890 I am here.


Edit - It seems that strangeity occurs when "gaining" is any hope.

: any = 1: any = swb.get val any2: any = b println ("any1:" + any1) // any 1: blank println ("any2:" + any2) / / Any 2: Incorrect

I'm pretty sure this is something with boxing / unboxing Antiquities If you write generic codes to work on primitives, then you should put that primitive box in the box, on which you used it as a primitive. I'm not sure which unblocking algorithm is used, but I think it is with the following lines:

  if (box == empty) default value else box.unbox < Therefore, I can add oddly  lot , the default value of the field  t  in your simple wrapper class always  null Going to , because the field is always going to be primitive boxing, because the generics are JVM Type is applied by type error. Therefore, all JVMs can be seen that with the value of  t  type of  object ,  null . Therefore  get  will always return  null , but when the normal method  is found  should return a primitive type, then  null  Default value is unboxed 

In addition, rotating around with a reflection actually shows that the field is actually null .

  val sw = new SimpleWrap [boolean] Sw.getClass.getDeclaredFields.map {f = & gt; F.setAccessible (true) f.get (sw)}  

Oh null s a solution to this problem 2.8 @specialised To use annotations, if it has been implemented in the night construction.

Or even better, these fields were used by default for the default default box of Scala compiler practices. For example, will be the type of the type of search [boolean] , t in the object and value java.lang.

Edit: Bug Report.

Another strange thing:

  Val x: int = null.asInstanceOf [int] // 0 y y: boolean = null. InstInOffice [Boolean] // false  

This is something that should be solved for generic, really normal, and be consistent! At the moment, your gets method does not have a coherent behavior.

- Flavio Cipiking


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -