1 Dec 2010 05:31
Re: What is going on here?
HamsterofDeath wrote:
> by intuition, i'd say that in case of a lazy val someunit = {moomooomooo()}
> a unit will be initialized on the first access (new boxedunit or
> whatever), but not yet executed. it's like the difference between
> calling a function and referencing a function
The decompiled code has a void method foo() that checks if bitmap$0 is
set, if not, calls println and sets bitmap$0.
> Am 30.11.2010 19:00, schrieb Ittay Dror:
>> It looks to me like creating a lazy val that implements an abstract
>> val does not work for unit vals.
>>
>>
>> See the call 'unitFoo.foo' in the transcript below:
>>
>>
>> Ittay
>>
>>
>> scala> trait Foo[T] {
>>
>> | val foo: T
>> | }
>> defined trait Foo
>>
>> scala>
>>
>> scala> val stringFoo = new Foo[String]{
>> | lazy val foo = {println("String: called "); "hi"}
>> | }
>> stringFoo: java.lang.Object with Foo[String] = $anon$1 <at> 181e7fe
>>
>> scala>
>>
>> scala> val unitFoo = new Foo[Unit]{
>> | lazy val foo = println("Unit: called ")
>> | }
>> unitFoo: java.lang.Object with Foo[Unit] = $anon$1 <at> 1de530a
>>
>> scala> stringFoo.foo
>> String: called
>> res0: String = hi
>>
>> scala> unitFoo.foo
>>
>>
>> scala> class Bar { lazy val foo = println("hi")}
>>
>> defined class Bar
>>
>>
>> scala> (new Bar).foo
>> hi
>>
>> scala>
>>
>>
RSS Feed