This page is no longer maintained — Please continue to the home page at www.scala-lang.org

surprised by class initialization

57 replies
Alec Zorab
Joined: 2010-05-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: surprised by class initialization

just tried this - I get 5/5 with final and 0/5 without. scala 2.9.1,
win7, java 6u29

On 9 December 2011 16:58, Eugen Labun wrote:
>> ... Try this one to see 5/5. ...
>>
>>
>> object Test {
>>  final val x = y // compiles OK
>>  final val y = 5
>>
>> def main(args: Array[String]) {
>>  println("x = " + x) // 5
>>  println("y = " + y) // 5
>>  }
>> }
>>
>>
>> C:\scala-2.9.1.final\examples>scalac testscala.scala
>>
>> C:\scala-2.9.1.final\examples>scala testscala.Test
>> x = 0
>> y = 5
>
>
> But you get 0/5, too (not 5/5). Or am I missing something?
>
>
>>
>> Are you testing with 2.10 ?
>
> I'm testing with Scala 2.9.1.final on Windows XP SP3.
>

E. Labun
Joined: 2010-06-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: surprised by class initialization

> But you get 0/5, too (not 5/5). Or am I missing something?
>
>>
>> Are you testing with 2.10 ?
> I'm testing with Scala 2.9.1.final on Windows XP SP3.

Oh, sorry. That was your answer to Pauls mail (which was not CCed to the forum).

The fact that we get different results from the same code make things much more interesting!

E. Labun
Joined: 2010-06-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: surprised by class initialization

On 2011-12-09 18:00, Alec Zorab wrote:
> just tried this - I get 5/5 with final and 0/5 without. scala 2.9.1,
> win7, java 6u29

And for me the type annotation ": Int" makes the difference!

This prints 5/0:
----------------------------------------
object Test {
final val x: Int = y // compiles OK
final val y: Int = 5

def main(args: Array[String]) {
println("x = " + x) // 0
println("y = " + y) // 5
}
}
----------------------------------------

And this 5/5:
----------------------------------------
object Test {
final val x = y // compiles OK
final val y = 5

def main(args: Array[String]) {
println("x = " + x) // 5
println("y = " + y) // 5
}
}
----------------------------------------

Wow!

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: surprised by class initialization

Ah, yes. There's that...

On Fri, Dec 9, 2011 at 15:12, Eugen Labun wrote:
> On 2011-12-09 18:00, Alec Zorab wrote:
>>
>> just tried this - I get 5/5 with final and 0/5 without. scala 2.9.1,
>> win7, java 6u29
>
>
> And for me the type annotation ": Int" makes the difference!
>
> This prints 5/0:
> ----------------------------------------
> object Test {
>    final val x: Int = y  // compiles OK
>    final val y: Int = 5
>
>
>    def main(args: Array[String]) {
>        println("x = " + x) // 0
>        println("y = " + y) // 5
>    }
> }
> ----------------------------------------
>
> And this 5/5:
> ----------------------------------------
>
> object Test {
>    final val x = y  // compiles OK
>    final val y = 5
>
>    def main(args: Array[String]) {
>        println("x = " + x) // 5
>        println("y = " + y) // 5
>    }
> }
> ----------------------------------------
>
> Wow!

DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
RE: Re: surprised by class initialization
Strange.
same with or without the package testscala doesn't matter
I have
Windows 7 Home Premium 32 bit + SP1
tested with
scala 2.9.1.final
jdk1.7_01
jdk1.6_18
 
> Date: Fri, 9 Dec 2011 18:05:15 +0100
> From: labun [at] gmx [dot] net
> To: dave [dot] mahabiersing [at] hotmail [dot] com
> CC: scala-user [at] googlegroups [dot] com
> Subject: Re: [scala-user] Re: surprised by class initialization
>
> > But you get 0/5, too (not 5/5). Or am I missing something?
> >
> >>
> >> Are you testing with 2.10 ?
> > I'm testing with Scala 2.9.1.final on Windows XP SP3.
>
> Oh, sorry. That was your answer to Pauls mail (which was not CCed to the forum).
>
> The fact that we get different results from the same code make things much more interesting!
DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
RE: Re: surprised by class initialization
For me too
C:\scala-2.9.1.final\examples>scala Test
x = 5
y = 5 
> Date: Fri, 9 Dec 2011 18:12:08 +0100
> From: labun [at] gmx [dot] net
> To: aleczorab [at] googlemail [dot] com
> CC: dave [dot] mahabiersing [at] hotmail [dot] com; scala-user [at] googlegroups [dot] com
> Subject: Re: [scala-user] Re: surprised by class initialization
>
> On 2011-12-09 18:00, Alec Zorab wrote:
> > just tried this - I get 5/5 with final and 0/5 without. scala 2.9.1,
> > win7, java 6u29
>
> And for me the type annotation ": Int" makes the difference!
>
> This prints 5/0:
> ----------------------------------------
> object Test {
> final val x: Int = y // compiles OK
> final val y: Int = 5
>
> def main(args: Array[String]) {
> println("x = " + x) // 0
> println("y = " + y) // 5
> }
> }
> ----------------------------------------
>
> And this 5/5:
> ----------------------------------------
> object Test {
> final val x = y // compiles OK
> final val y = 5
>
> def main(args: Array[String]) {
> println("x = " + x) // 5
> println("y = " + y) // 5
> }
> }
> ----------------------------------------
>
> Wow!
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: surprised by class initialization

On Fri, Dec 9, 2011 at 15:05, Eugen Labun wrote:
>> But you get 0/5, too (not 5/5). Or am I missing something?
>>
>>>
>>> Are you testing with 2.10 ?
>>
>> I'm testing with Scala 2.9.1.final on Windows XP SP3.
>
>
> Oh, sorry. That was your answer to Pauls mail (which was not CCed to the
> forum).
>
> The fact that we get different results from the same code make things much
> more interesting!

On http://www.simplyscala.com/:

Creating user space...
Ready for code.
object Test {
final val x = y // compiles OK
final val y = 5

def main(args: Array[String]) {
println("x = " + x) // 5
println("y = " + y) // 5
}
}
defined module Test
Test.main(null)
x = 5
y = 5

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland