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

enlightning REPL session

5 replies
Luc Duponcheel
Joined: 2008-12-19,
User offline. Last seen 34 weeks 3 days ago.

Guys,

while dealing with more complex stuff
I found myself confronted with semantics
that, in essence, corresponds to the results
*not* shown in the following REPL session

maybe you should try to find what (if anything) the ????'s might be

[ I thought it was useful to share this ]

==== begin ====

scala> trait Handler {
| def handle(): Unit
| }
defined trait Handler

scala> implicit def blockToHandler(block: => Unit) = new Handler {
| def handle() {
| block
| }
| }
blockToHandler: (block: => Unit)java.lang.Object with Handler

scala> val blockHandler: Handler = {
| println("one")
| println("two")
| }
????
blockHandler: Handler = $anon$1@191394e

scala> blockHandler.handle()
????

scala> implicit def blockToHandler(block:Unit) = new Handler {
| def handle() {
| block
| }
| }
blockToHandler: (block: Unit)java.lang.Object with Handler

scala> val blockHandler: Handler = {
| println("one")
| println("two")
| }
????
blockHandler: Handler = $anon$1@159d450

scala> blockHandler.handle()
????

scala>

===== end =====

Luc

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: enlightning REPL session

On Fri, Jan 20, 2012 at 19:54, Luc Duponcheel wrote:
> Guys,
>
> while dealing with more complex stuff
> I found myself confronted with semantics
> that, in essence, corresponds to the results
> *not* shown in the following REPL session
>
> maybe you should try to find what (if anything) the ????'s might be

I'm guessing one, two, one and two and nothing, in that order.

Scala has no such thing as a block of code as a value. People then get
confused because they think of "=> Unit" as being an alias for the
non-existent "block of code". Call by name is call by name, and a
block is a series of declarations and expressions, whose *value* is
equal to that of the last statement.

So, if you have an implicit from "=> Unit", and you pass a block, it
will pass by name the value of that block: the last statement.

>
> [ I thought it was useful to share this ]
>
> ==== begin ====
>
> scala> trait Handler {
>     |  def handle(): Unit
>     | }
> defined trait Handler
>
> scala> implicit def blockToHandler(block: => Unit) = new Handler {
>     |  def handle() {
>     |   block
>     |  }
>     | }
> blockToHandler: (block: => Unit)java.lang.Object with Handler
>
> scala> val blockHandler: Handler = {
>     |  println("one")
>     |  println("two")
>     | }
> ????
> blockHandler: Handler = $anon$1@191394e
>
> scala> blockHandler.handle()
> ????
>
> scala> implicit def blockToHandler(block:Unit) = new Handler {
>     |  def handle() {
>     |   block
>     |  }
>     | }
> blockToHandler: (block: Unit)java.lang.Object with Handler
>
> scala> val blockHandler: Handler = {
>     |  println("one")
>     |  println("two")
>     | }
> ????
> blockHandler: Handler = $anon$1@159d450
>
> scala> blockHandler.handle()
> ????
>
> scala>
>
>
> =====   end   =====
>
>
> Luc

Luc Duponcheel
Joined: 2008-12-19,
User offline. Last seen 34 weeks 3 days ago.
Re: enlightning REPL session

Daniel,

indeed

but, sometimes, the usage (via a DSL) of the code that I was
showing would naturally suggest stuff like:

onMouseEntered = {
println("mouse entered")
scaleX = 1.5
scaleY = 1.5
}

(where the rhs is the "block of code")

this is what I encountered when using the ScalaFX DSL
the way I intuitively (and somewhat naively) thought it
could be used

Luc

On Jan 20, 11:02 pm, Daniel Sobral wrote:
> On Fri, Jan 20, 2012 at 19:54, Luc Duponcheel wrote:
> > Guys,
>
> > while dealing with more complex stuff
> > I found myself confronted with semantics
> > that, in essence, corresponds to the results
> > *not* shown in the following REPL session
>
> > maybe you should try to find what (if anything) the ????'s might be
>
> I'm guessing one, two, one and two and nothing, in that order.
>
> Scala has no such thing as a block of code as a value. People then get
> confused because they think of "=> Unit" as being an alias for the
> non-existent "block of code". Call by name is call by name, and a
> block is a series of declarations and expressions, whose *value* is
> equal to that of the last statement.
>
> So, if you have an implicit from "=> Unit", and you pass a block, it
> will pass by name the value of that block: the last statement.
>
>
>
>
>
>
>
>
>
>
>
> > [ I thought it was useful to share this ]
>
> > ==== begin ====
>
> > scala> trait Handler {
> >     |  def handle(): Unit
> >     | }
> > defined trait Handler
>
> > scala> implicit def blockToHandler(block: => Unit) = new Handler {
> >     |  def handle() {
> >     |   block
> >     |  }
> >     | }
> > blockToHandler: (block: => Unit)java.lang.Object with Handler
>
> > scala> val blockHandler: Handler = {
> >     |  println("one")
> >     |  println("two")
> >     | }
> > ????
> > blockHandler: Handler = $anon$1@191394e
>
> > scala> blockHandler.handle()
> > ????
>
> > scala> implicit def blockToHandler(block:Unit) = new Handler {
> >     |  def handle() {
> >     |   block
> >     |  }
> >     | }
> > blockToHandler: (block: Unit)java.lang.Object with Handler
>
> > scala> val blockHandler: Handler = {
> >     |  println("one")
> >     |  println("two")
> >     | }
> > ????
> > blockHandler: Handler = $anon$1@159d450
>
> > scala> blockHandler.handle()
> > ????
>
> > scala>
>
> > =====   end   =====
>
> > Luc
>
> --
> Daniel C. Sobral
>
> I travel to the future all the time.

Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: enlightning REPL session

> onMouseEntered = {
> println("mouse entered")
> scaleX = 1.5
> scaleY = 1.5
> }

I guess you already know this, but just for the records: If you don't
make the conversion implicit, but require an explicit call, it works "as
expected", e. g.

onMouseEntered = handler {
println("mouse entered")
// ...
}

As always, the other approach is to be explicit about your effects (i.
e. let the type system help you).

vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: enlightning REPL session
Luc,

Basically, as Daniel pointed out, a bunch of parentheses missing.

scala> trait Handler { def handle(): Unit }
defined trait Handler

scala> implicit def b2Handler(b:() => Unit) = new Handler {
     | def handle() { b() }}
b2Handler: (b: () => Unit)java.lang.Object with Handler

scala> val blockHandler: Handler = () => {println("one"); println("two") }
blockHandler: Handler = $anon$1@de35b38

scala> blockHandler.handle
one
two

Thanks,
-Vlad


On Fri, Jan 20, 2012 at 1:54 PM, Luc Duponcheel <luc.duponcheel@gmail.com> wrote:
Guys,

while dealing with more complex stuff
I found myself confronted with semantics
that, in essence, corresponds to the results
*not* shown in the following REPL session

maybe you should try to find what (if anything) the ????'s might be

[ I thought it was useful to share this ]

==== begin ====

scala> trait Handler {
    |  def handle(): Unit
    | }
defined trait Handler

scala> implicit def blockToHandler(block: => Unit) = new Handler {
    |  def handle() {
    |   block
    |  }
    | }
blockToHandler: (block: => Unit)java.lang.Object with Handler

scala> val blockHandler: Handler = {
    |  println("one")
    |  println("two")
    | }
????
blockHandler: Handler = $anon$1@191394e

scala> blockHandler.handle()
????

scala> implicit def blockToHandler(block:Unit) = new Handler {
    |  def handle() {
    |   block
    |  }
    | }
blockToHandler: (block: Unit)java.lang.Object with Handler

scala> val blockHandler: Handler = {
    |  println("one")
    |  println("two")
    | }
????
blockHandler: Handler = $anon$1@159d450

scala> blockHandler.handle()
????

scala>


=====   end   =====


Luc

Luc Duponcheel
Joined: 2008-12-19,
User offline. Last seen 34 weeks 3 days ago.
Re: enlightning REPL session

thx for the remark

On Jan 21, 11:54 pm, Lars Hupel wrote:
> >     onMouseEntered = {
> >       println("mouse entered")
> >       scaleX = 1.5
> >       scaleY = 1.5
> >     }
>
> I guess you already know this, but just for the records: If you don't
> make the conversion implicit, but require an explicit call, it works "as
> expected", e. g.
>
> onMouseEntered = handler {
>   println("mouse entered")
>   // ...
>
> }
>
> As always, the other approach is to be explicit about your effects (i.
> e. let the type system help you).

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