scala> val n = null.asInstanceOf[Int]
n: Int = 0
scala> val n = null.asInstanceOf[Double]
n: Double = 0.0
I'd expect it to fail in compilation. AnnUal extends NotNull. In Java it fails in runtime.
Works as expected.--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
asInstanceOf
works on AnyRef
, not onAnyVal
. When applied to an AnyVal
, it will use the boxed version instead, and a boxed null
has value 0." -- Daniel SobralI'd expect it to fail in compilation. AnnUal extends NotNull. In Java it fails in runtime.
On Aug 27, 2016 2:30 AM, "Simon Ochsenreither" <simon.och...@gmail.com> wrote:
Works as expected.--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
I'd expect it to fail in compilation
"Now, if you want to know why it returns 0, that's becauseasInstanceOf
works onAnyRef
, not onAnyVal
. When applied to anAnyVal
, it will use the boxed version instead, and a boxednull
has value 0." -- Daniel Sobral
On Sat, Aug 27, 2016 at 9:54 AM Vlad Patryshev <vpatr...@gmail.com> wrote:
I'd expect it to fail in compilation. AnnUal extends NotNull. In Java it fails in runtime.
On Aug 27, 2016 2:30 AM, "Simon Ochsenreither" <simon.och...@gmail.com> wrote:
Works as expected.--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
Thanks,
-Vlad
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
Ideally we would have something like defaultOf[T], that would relieve null.asInstanceOf[T] from some of its duties, but nobody had enough time to propose and implement it.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
trait HasDefault[T] { def default: T }object HasDefault {object IntHasDefault extends HasDefault[Int] { val default = 0 }object LongHasDefault extends HasDefault[Long] { val default = 0L }object DoubleHasDefault extends HasDefault[Double] { val default = 0.0d }object StringHasDefault extends HasDefault[String] { val default = “" }…def defaultOf[T](implicit ev: HasDefault[T]) = ev.default}
import HasDefault._println(defaultOf[Int])
“Beware that, when fighting monsters, you yourself do not become a monster... for when you gaze long into the abyss. The abyss gazes also into you.” ~ Friedrich Nietzsche
I guess that's why null.asInstanceOf[Int] is 0.It actually chooses null as default value for x, even though x is Int. And when you call A.x, it casts null to Int and get 0.Until you take a closer look:Oops, hit sent by mistake.Anyway, at first glance, it seems to work just fine:
scala> class A[T] { var x:T = _ }
defined class A
scala> val aInt = new A[Int]
aInt: A[Int] = A@36f6e879
scala> aInt.x
res2: Int = 0
scala> class A[T] { var x:T = _; println(x) }
defined class A
scala> val aInt = new A[Int]
null
aInt: A[Int] = A@3fee9989
scala> aInt.x
res3: Int = 0On the other hand, that bolsters the case for an actually working defaultOf[T]. The drawback: it needs a type tag and therefore needs to be part of the Scala Reflection library. But there, I think it would be useful.
Thoughts?Best, Oliver
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
I'm trying to understand what "boxed null has value 0" means.Hello,
Best, OliverIf boxed null has value 0, why I don't get 0?The term "boxed null" is a bit misleading, because it makes it sound as if there was a box with a null inside, but in fact, the box itself is null. If a box is null and I ask what is inside, why is this not NullPointerException?Do I ever get 0 other than with isInstanceOf? For example:Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.
scala> Double2double(null)
java.lang.NullPointerException
at scala.Predef$.Double2double(Predef.scala:365)
... 32 elided
On Sat, Aug 27, 2016 at 10:06 AM, Arya Irani <arya....@gmail.com> wrote:
"Now, if you want to know why it returns 0, that's becauseasInstanceOf
works onAnyRef
, not onAnyVal
. When applied to anAnyVal
, it will use the boxed version instead, and a boxednull
has value 0." -- Daniel Sobral
On Sat, Aug 27, 2016 at 9:54 AM Vlad Patryshev <vpatr...@gmail.com> wrote:
I'd expect it to fail in compilation. AnnUal extends NotNull. In Java it fails in runtime.
On Aug 27, 2016 2:30 AM, "Simon Ochsenreither" <simon.och...@gmail.com> wrote:
Works as expected.--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
Hello,
That is indeed funny that 1.asInstanceOf[Long] does not compile, but null.asInstanceOf[Int] compiles and runs without exception.
I disagree with Daniel Sobral that asInstanceOf "does not (have to) make sense". It is clearly defined as a cast, so a.asInstanceOf[B] clearly means "the compiler should now treat a as of type B, even if the compiler can't prove it".
If the cast is incorrect (a is not of type B), I would expect it to throw a class cast exception.
Moreover, if the compiler can prove that the cast is incorrect, it should report an error.
I see Rex Kerr says the current behaviour is intended to make this:
var x: X = _
But I don't see why we want that. If the idea is that null is a default value and therefore it should cast to a default value, I don't see that as consistent, because other default values like 0, 0L or false can't be cast to other default values either (0.asInstanceOf[AnyRef] is not null).var x: X = null.asInstanceOf[X]the same as this:
Best, Oliver
Thanks,
-Vlad
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
On Tue, Aug 30, 2016, 6:54 AM Oliver Ruebenacker <cur...@gmail.com> wrote:I guess that's why null.asInstanceOf[Int] is 0.It actually chooses null as default value for x, even though x is Int. And when you call A.x, it casts null to Int and get 0.Until you take a closer look:Oops, hit sent by mistake.Anyway, at first glance, it seems to work just fine:
scala> class A[T] { var x:T = _ }
defined class A
scala> val aInt = new A[Int]
aInt: A[Int] = A@36f6e879
scala> aInt.x
res2: Int = 0
scala> class A[T] { var x:T = _; println(x) }
defined class A
scala> val aInt = new A[Int]
null
aInt: A[Int] = A@3fee9989
scala> aInt.x
res3: Int = 0On the other hand, that bolsters the case for an actually working defaultOf[T]. The drawback: it needs a type tag and therefore needs to be part of the Scala Reflection library. But there, I think it would be useful.TypeTag is a typeclass representing metadata about a type. Why not have a dedicated typeclass instead?
Thoughts?Best, Oliver
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thoughts?Best, Oliver
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
it's in the JVM spec which are the default values for fields.
--
Cheers,
√
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
it's in the JVM spec which are the default values for fields.
--
Cheers,
√
Roland
Thanks,
-Vlad
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
Kevin Wright
mail / hangouts / msn : kev.lee...@gmail.comvibe / skype: kev.lee.wright"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
Is var x: X = _ generating initialization code or not?
--
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
(1) invent a new operationSo, you couldBut in Scala, if `T` is `Any`, you are stuck. You _mean_ "just fill in the sensible default". But you don't have any value you can fill in.When dealing with generic code, Java has only `Object` while Scala has both `Any` and `AnyRef`.If you are writing generic code in Java, you can always say `T thing = null` and it will at least not blow up in your face.
(2) decide that casting a `null` to an AnyVal type produces the default value for that type(3) provide nothing, which encourages extra boilerplate and/or error-prone code
Keep in mind that we have to decide what to do on _all_ `asInstanceOf`s. What does7.asInstanceOf[AnyRef]mean? Should it explode later?
But how? Since surely7.asInstanceOf[Any]ought to produce the same behavior as7: Any
but the way this is achieved is precisely by boxing it as an AnyRef.
So your hands are already tied, given the inheritance hierarchy: if asInstanceOf is going to work on Any at all, you're going to have to allow it to box/unbox things as needed.
So then the only question remaining is how you box/unbox `null`. Your only real choices are
a) NullPointerExceptionb) JVM-mandated default value
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
Hello,var x: X = _If X is known, it can just insert the correct default value. If X is an unbound type parameter, it will initialize it to null and cast it if called from a place where X is known:
Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help.scala> class A[X] { var x: X = _; println(s"The value of x is $x") }This is where null.asInstanceOf[Int] comes in behind the scene.
defined class A
scala> (new A[Int]).x
The value of x is null
res1: Int = 0
Best, Oliver
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
But in Scala, if `T` is `Any`, you are stuck. You _mean_ "just fill in the sensible default". But you don't have any value you can fill in.When dealing with generic code, Java has only `Object` while Scala has both `Any` and `AnyRef`.If you are writing generic code in Java, you can always say `T thing = null` and it will at least not blow up in your face.
(3) provide nothing, which encourages extra boilerplate and/or error-prone code(2) decide that casting a `null` to an AnyVal type produces the default value for that typeSo, you could(1) invent a new operation
Keep in mind that we have to decide what to do on _all_ `asInstanceOf`s. What does7.asInstanceOf[AnyRef]mean? Should it explode later? But how? Since surely
7.asInstanceOf[Any]ought to produce the same behavior as7: Anybut the way this is achieved is precisely by boxing it as an AnyRef.
So your hands are already tied, given the inheritance hierarchy: if asInstanceOf is going to work on Any at all, you're going to have to allow it to box/unbox things as needed.So then the only question remaining is how you box/unbox `null`. Your only real choices area) NullPointerExceptionb) JVM-mandated default value
If you want any code that works with `AnyRef` to also work with `Any`, you can't just throw exceptions. So you have to use the default value.
On Wed, Aug 31, 2016 at 5:11 PM Rex Kerr <ich...@gmail.com> wrote:(1) invent a new operationSo, you couldBut in Scala, if `T` is `Any`, you are stuck. You _mean_ "just fill in the sensible default". But you don't have any value you can fill in.When dealing with generic code, Java has only `Object` while Scala has both `Any` and `AnyRef`.If you are writing generic code in Java, you can always say `T thing = null` and it will at least not blow up in your face.I guess you're alluding to the `defaultOf[T]` / `zeroOf[T]` idea in this thread?
(2) decide that casting a `null` to an AnyVal type produces the default value for that type(3) provide nothing, which encourages extra boilerplate and/or error-prone codeCurious what the error-prone solution would be.
I suppose the "extra boilerplate" solution is what I mentioned earlier, namely requiring you to get the default value as a parameter. That parameter could be implicit -- it could be a typeclass. And `zeroOf` could be implemented via a typeclass. So we could unify #1 and #3...Keep in mind that we have to decide what to do on _all_ `asInstanceOf`s. What does7.asInstanceOf[AnyRef]mean? Should it explode later?Why not explode right away?
But how? Since surely7.asInstanceOf[Any]ought to produce the same behavior as7: AnyWhy?
Type ascription can invoke views (such as implicit conversions or type widening). I don't letting them box and unbox (although those are outside the scope of the type system). Casting doesn't invoke views. For example "".asInstanceOf[WrappedString] throws a CCE.
And I think that's logical, because casting tells the compiler to discard its conclusions and submit to our assertions. So I don't see that the two should behave the same.but the way this is achieved is precisely by boxing it as an AnyRef.The way what is achieved?
So your hands are already tied, given the inheritance hierarchy: if asInstanceOf is going to work on Any at all, you're going to have to allow it to box/unbox things as needed.I think the boxing happens before getting to the asInstanceOf, no? Does 7.asInstanceOf[AnyRef] mean call the "method" Any#asInstanceOf on 7, and that method's implementation does boxing? Or, asInstanceOf can only be "called" on a boxed value, so the compiler inserts a box and the "method call" on that?
So then the only question remaining is how you box/unbox `null`. Your only real choices area) NullPointerExceptionb) JVM-mandated default valuenull is an AnyRef, why would you box it or unbox it?
Hello,On Wed, Aug 31, 2016 at 5:11 PM, Rex Kerr <ich...@gmail.com> wrote:But in Scala, if `T` is `Any`, you are stuck. You _mean_ "just fill in the sensible default". But you don't have any value you can fill in.When dealing with generic code, Java has only `Object` while Scala has both `Any` and `AnyRef`.If you are writing generic code in Java, you can always say `T thing = null` and it will at least not blow up in your face.Maybe it should not be legal to create a default value if you don't know what type it is.
(3) provide nothing, which encourages extra boilerplate and/or error-prone code(2) decide that casting a `null` to an AnyVal type produces the default value for that typeSo, you could(1) invent a new operationIf we could provide something similar to TypeTag/ClassManifest, but more lightweight, and part of the standard library (not the reflectio library). Let's call it TypeFamily, and it only tracks whether something is AnyRef, Int, Long, Short, Char, Float, Double, Byte or Boolean. You could use that to create default values or arrays.
Keep in mind that we have to decide what to do on _all_ `asInstanceOf`s. What does7.asInstanceOf[AnyRef]mean? Should it explode later? But how? Since surelyClassCastException. 7 is not an AnyRef.
7.asInstanceOf[Any]ought to produce the same behavior as7: Anybut the way this is achieved is precisely by boxing it as an AnyRef.AnyRef is a static type. Boxing and unboxing is an implementation detail. An AnyVal is not an AnyRef, even if an AnyRef is sometimes used behind the scenes to implement it.
So your hands are already tied, given the inheritance hierarchy: if asInstanceOf is going to work on Any at all, you're going to have to allow it to box/unbox things as needed.So then the only question remaining is how you box/unbox `null`. Your only real choices area) NullPointerExceptionb) JVM-mandated default valuec) ClassCastException
On Thu, Sep 1, 2016 at 2:24 AM, Oliver Ruebenacker <cur...@gmail.com> wrote:Hello,On Wed, Aug 31, 2016 at 5:11 PM, Rex Kerr <ich...@gmail.com> wrote:But in Scala, if `T` is `Any`, you are stuck. You _mean_ "just fill in the sensible default". But you don't have any value you can fill in.When dealing with generic code, Java has only `Object` while Scala has both `Any` and `AnyRef`.If you are writing generic code in Java, you can always say `T thing = null` and it will at least not blow up in your face.Maybe it should not be legal to create a default value if you don't know what type it is.This makes for rather painful code when you have several generics to initialize but you don't know the order in which you'll find them.
(3) provide nothing, which encourages extra boilerplate and/or error-prone code(2) decide that casting a `null` to an AnyVal type produces the default value for that typeSo, you could(1) invent a new operationIf we could provide something similar to TypeTag/ClassManifest, but more lightweight, and part of the standard library (not the reflectio library). Let's call it TypeFamily, and it only tracks whether something is AnyRef, Int, Long, Short, Char, Float, Double, Byte or Boolean. You could use that to create default values or arrays.Not useful if everything is typed as Any. It still needs to behave sanely then.
Keep in mind that we have to decide what to do on _all_ `asInstanceOf`s. What does7.asInstanceOf[AnyRef]mean? Should it explode later? But how? Since surelyClassCastException. 7 is not an AnyRef.Well, java.lang.Integer(7) is an AnyRef. And guess what (7: Any) is? And if (7: Any) can do something, 7 should be able to also according to LSP.
7.asInstanceOf[Any]ought to produce the same behavior as7: Anybut the way this is achieved is precisely by boxing it as an AnyRef.AnyRef is a static type. Boxing and unboxing is an implementation detail. An AnyVal is not an AnyRef, even if an AnyRef is sometimes used behind the scenes to implement it.The type system doesn't let you completely escape the consequences of this implementation detail.So your hands are already tied, given the inheritance hierarchy: if asInstanceOf is going to work on Any at all, you're going to have to allow it to box/unbox things as needed.So then the only question remaining is how you box/unbox `null`. Your only real choices area) NullPointerExceptionb) JVM-mandated default valuec) ClassCastExceptionSee my answer to Naftoli. Except note that CCE is even less consistent.--Rex
--Rex
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
I think this conversation is going on a bit too long (I agree that things can be different with boilerplate). But just to demonstrate why, given implementation details, things aren't easy:
Givenclass Sloppy[A] { var value: A = _ }This really really ought to work:{ val s = new Sloppy[String]; s.value = null }.value.asInstanceOf[String]But you don't know whether the thing in parens has happened, which makes all of these (which in practice assign a null) equivalent:
(new Sloppy[String]).value.asInstanceOf[String]
(new Sloppy[Option[Boolean]]).value.asInstanceOf[String]
(new Sloppy[Float]).value.asInstanceOf[String]So they must have the same behavior.
For consistency's sake you expect
(new Sloppy[Float]).value.asInstanceOf[Float]to at least not throw an exception, since the String/String version doesn't.
and so you'd better have a way to initialize your Float from a null.But this is indistinguishable from(new Sloppy[Option[Boolean]]).value.asInstanceOf[Float](new Sloppy[String]).value.asInstanceOf[Float]
But it also makes some sense that it is the way it is.Now you _could_ say that primitives randomly break or you need a bunch of typeclasses to do any initialization (and indeed you _do_ need a bunch of typeclasses to initialize arrays, and it is a pain).
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.