Skip to content

Instantly share code, notes, and snippets.

@lucamolteni
Created February 18, 2015 16:17
Show Gist options
  • Save lucamolteni/dbb49027b02851caf928 to your computer and use it in GitHub Desktop.
Save lucamolteni/dbb49027b02851caf928 to your computer and use it in GitHub Desktop.
[17:06:42] volothamp: simple question: why does this expression evalute to null?
[17:06:44] volothamp: scala> val x: Integer = if (0 != 1) x else 42
[17:06:44] volothamp: x: Integer = null
[17:06:58] darkfrog: hehe
[17:07:14] darkfrog: volothamp: you're declaring x and assigning it x dude
[17:07:59] darkfrog: volothamp: I have no idea what you're trying to do, but you're doing it wrong. :-p
[17:08:10] volothamp: darkfrog: it's an edge case :p
[17:08:17] arjen-jonathan: I think that the compiler is telling you:
[17:08:30] darkfrog: volothamp: it's not an edge case, it's bad code
[17:08:44] TKH (~TKH@p1139-ipbf1009souka.saitama.ocn.ne.jp) joined the channel
[17:08:51] arjen-jonathan: "Well... you're type is fine... but the only value that I can find that kinda does what you do is null"
[17:08:54] volothamp: darkfrog: ok, it's a bad code. I'm just wondering why that is
[17:09:02] oliffia: x is initialized to a default value before the RHS is evaluated, IIRC
[17:09:09] oliffia: similar to fields in java
[17:09:14] stew: volothamp: you are initializing a value with its uninitialized self, this should probably just not be allowed
[17:09:22] arjen-jonathan: specifically, the interpreter is warning you: warning: value x does nothing other than call itself recursively
[17:09:34] arjen-jonathan: ! val x: Integer = x
[17:09:35] multibot_: <console>:24: warning: value x does nothing other than call itself recursively
[17:09:36] multibot_: val x: Integer = x
[17:09:37] multibot_: ^
[17:09:38] multibot_: x: Integer = null
[17:09:38] oliffia: it absolutely should not be allowed, but scala lets you :C
[17:09:53] volothamp: because as an example, in haskell this doesn't terminate
[17:10:07] oliffia: haskell has nonstrictness, though
[17:10:23] folone (~folone@80.82.202.196) left IRC (Remote host closed the connection)
[17:10:28] volothamp: oliffia: can you elaborate more please?
[17:10:53] oliffia: in haskell it is more similar to the situation of def x:Integer = if (0 != 1) x else 42
[17:11:34] volothamp: oliffia: you are right. That doesn't terminate in scala too.
[17:12:46] oliffia: i believe this happens because scala uses java's underlying object model for object initialization. all fields are initialized to their default values first, and then the RHS of the member assignment is evaluated, if it exists and then the variable gets that value
[17:13:03] oliffia: after *all* members have been initialized with defaults
[17:13:15] marioosh (~marioosh@cw57.internetdsl.tpnet.pl) joined the channel
[17:13:30] oliffia: this lets you annoyingly do things like class X { val y = x; val x = 1 }
[17:14:04] volothamp: oliffia: in that case, y is 1 or null?
[17:14:22] oliffia: y will be zero, because the default for ints is 0
[17:14:32] oliffia: if these were object (reference) types, it would indeed be null
[17:14:40] Ven (~textual@163.5.245.253) left IRC (Quit: My MacBook has gone to sleep. ZZZzzz…)
[17:14:42] volothamp: oliffia: ok thank you
[17:14:48] huorpalantir (~adriaan@90-145-31-194.bbserv.nl) left IRC (Quit: Lost terminal)
[17:14:59] volothamp: another fun stuff. The very same line crashes the compiler
[17:15:13] volothamp: override def main (args: Array[String]) {
[17:15:13] volothamp: val x: Integer = if (0 != 1) x else 42
[17:15:13] volothamp: System.out.println("x is:" + x)
[17:15:13] volothamp: }
[17:15:22] volothamp: oops I should a gist for this
[17:15:25] volothamp: sorry
[17:15:34] oliffia: interesting, it even allows that for local variables?
[17:15:42] bblfish (~bblfish@78.193.235.5) joined the channel
[17:15:47] volothamp: https://gist.github.com/volothamp/bb4a2ef0f9f2e37b0af7
[17:16:17] oliffia: yep crashes mine too
[17:16:27] oliffia: everything i said only applies to class fields, not locals
[17:16:29] volothamp: oliffia: probably it doesn't: that's why the compiler crashes
[17:16:40] oliffia: no clue what logic is going on with locals
[17:16:44] volothamp: if you move the declaration outside the method the result is null indeed
[17:17:00] volothamp: that makes sense, thank you again :)
[17:17:20] folone (~folone@80.82.202.196) joined the channel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment