- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
enlightning REPL session
Fri, 2012-01-20, 22:54
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
Sat, 2012-01-21, 10:51
#2
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.
Sun, 2012-01-22, 00:01
#3
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).
Mon, 2012-01-23, 05:31
#4
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:
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
Tue, 2012-01-24, 21:21
#5
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).
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