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

I haz a funny

5 replies
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
What does the following mean:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait F { @volatile def F: Unit }
defined trait F

scala> new F { var F = () }
res0: java.lang.Object with F{def F_=(x$1: Unit): Unit} = $anon$1@50be0f51

scala> new F { val F = () }
res1: java.lang.Object with F = $anon$1@90901ce

scala> new F { def F = () }
res2: java.lang.Object with F = $anon$1@7d993bfd

--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: I haz a funny

2011/10/13 √iktor Ҡlang :
> What does the following mean:
>
> Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM,
> Java 1.6.0_26).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> trait F { @volatile def F: Unit }
> defined trait F
>
> scala> new F { var F = () }
> res0: java.lang.Object with F{def F_=(x$1: Unit): Unit} = $anon$1@50be0f51

res0's type is F plus an assignment method. Is that it?

>
> scala> new F { val F = () }
> res1: java.lang.Object with F = $anon$1@90901ce
>
> scala> new F { def F = () }
> res2: java.lang.Object with F = $anon$1@7d993bfd
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe - Enterprise-Grade Scala from the Experts
>
> Twitter: @viktorklang
>

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: I haz a funny


On Thu, Oct 13, 2011 at 2:20 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
2011/10/13 √iktor Ҡlang <viktor [dot] klang [at] gmail [dot] com>:
> What does the following mean:
>
> Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM,
> Java 1.6.0_26).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> trait F { @volatile def F: Unit }
> defined trait F
>
> scala> new F { var F = () }
> res0: java.lang.Object with F{def F_=(x$1: Unit): Unit} = $anon$1@50be0f51

res0's type is F plus an assignment method. Is that it?

No, my question is: What is the semantics of a @volatile def?
 

>
> scala> new F { val F = () }
> res1: java.lang.Object with F = $anon$1@90901ce
>
> scala> new F { def F = () }
> res2: java.lang.Object with F = $anon$1@7d993bfd
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe - Enterprise-Grade Scala from the Experts
>
> Twitter: @viktorklang
>



--
Daniel C. Sobral

I travel to the future all the time.



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
f.esser
Joined: 2009-11-23,
User offline. Last seen 42 years 45 weeks ago.
Re: I haz a funny
I doubt the usefulness of val and def together with volatile. 
From my understanding volatile control the visibility of a state changes of a volatile variable (including dependent variables), if one thread is writing the volatile variable that others are reading (i.e. checking for state change). It behaves like synchronized without guarantee of atomic updates.
So only the var F case seems to do something useful for coordinating threads (without locking). But the question is – as no state change is possible – if that has a useful implication. May be – even a write of the same value causes the proper visibility of dependent states (by cache update, etc.). But I would not count on it.
best regardsesser


2011/10/13 √iktor Ҡlang <viktor [dot] klang [at] gmail [dot] com>


On Thu, Oct 13, 2011 at 2:20 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
2011/10/13 √iktor Ҡlang <viktor [dot] klang [at] gmail [dot] com>:
> What does the following mean:
>
> Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM,
> Java 1.6.0_26).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> trait F { @volatile def F: Unit }
> defined trait F
>
> scala> new F { var F = () }
> res0: java.lang.Object with F{def F_=(x$1: Unit): Unit} = $anon$1@50be0f51

res0's type is F plus an assignment method. Is that it?

No, my question is: What is the semantics of a @volatile def?
 

>
> scala> new F { val F = () }
> res1: java.lang.Object with F = $anon$1@90901ce
>
> scala> new F { def F = () }
> res2: java.lang.Object with F = $anon$1@7d993bfd
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe - Enterprise-Grade Scala from the Experts
>
> Twitter: @viktorklang
>



--
Daniel C. Sobral

I travel to the future all the time.



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang


f.esser
Joined: 2009-11-23,
User offline. Last seen 42 years 45 weeks ago.
Re: I haz a funny
One more thing I forgot to mention:
In JMM (java memory model) volatile ensures a happens-before relation. I remember this one:
Writing of a volatile declared field happens before subsequent reading of that field. Alike reading a volatile field happens before  subsequent writing. So code reordering of volatile fields are forbidden. May be this it you are after. But without looking at the (java) code, the scala compiler will generates, I would not count on it.
best regardsesser

2011/10/13 friedrich esser <esser [dot] friedrich [at] gmail [dot] com>
I doubt the usefulness of val and def together with volatile. 
From my understanding volatile control the visibility of a state changes of a volatile variable (including dependent variables), if one thread is writing the volatile variable that others are reading (i.e. checking for state change). It behaves like synchronized without guarantee of atomic updates.
So only the var F case seems to do something useful for coordinating threads (without locking). But the question is – as no state change is possible – if that has a useful implication. May be – even a write of the same value causes the proper visibility of dependent states (by cache update, etc.). But I would not count on it.
best regardsesser


2011/10/13 √iktor Ҡlang <viktor [dot] klang [at] gmail [dot] com>


On Thu, Oct 13, 2011 at 2:20 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
2011/10/13 √iktor Ҡlang <viktor [dot] klang [at] gmail [dot] com>:
> What does the following mean:
>
> Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM,
> Java 1.6.0_26).
> Type in expressions to have them evaluated.
> Type :help for more information.
>
> scala> trait F { @volatile def F: Unit }
> defined trait F
>
> scala> new F { var F = () }
> res0: java.lang.Object with F{def F_=(x$1: Unit): Unit} = $anon$1@50be0f51

res0's type is F plus an assignment method. Is that it?

No, my question is: What is the semantics of a @volatile def?
 

>
> scala> new F { val F = () }
> res1: java.lang.Object with F = $anon$1@90901ce
>
> scala> new F { def F = () }
> res2: java.lang.Object with F = $anon$1@7d993bfd
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe - Enterprise-Grade Scala from the Experts
>
> Twitter: @viktorklang
>



--
Daniel C. Sobral

I travel to the future all the time.



--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang




nilskp
Joined: 2009-01-30,
User offline. Last seen 1 year 27 weeks ago.
Re: I haz a funny
On Thu, Oct 13, 2011 at 7:21 AM, √iktor Ҡlang <viktor [dot] klang [at] gmail [dot] com> wrote:
No, my question is: What is the semantics of a @volatile def?

Must be a compiler bug. Makes no sense.

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