What's the difference between => , ()=>, and Unit=>
Posted
by
Malvolio
on Stack Overflow
See other posts from Stack Overflow
or by Malvolio
Published on 2010-12-28T02:14:58Z
Indexed on
2010/12/28
2:54 UTC
Read the original article
Hit count: 140
scala
I'm trying to represent a function that takes no arguments and returns no value (I'm simulating the setTimeout function in JavaScript, if you must know.)
case class Scheduled(time : Int, callback : => Unit)
doesn't compile, saying " `val' parameters may not be call-by-name"
case class Scheduled(time : Int, callback : () => Unit)
compiles, but has to be invoked strangely, instead of
Scheduled(40, { println("x") } )
I have to do this
Scheduled(40, { () => println("x") } )
What also works is
class Scheduled(time : Int, callback : Unit => Unit)
but is invoked in an even-less-sensible way
Scheduled(40, { x : Unit => println("x") } )
(What would a variable of type Unit be?) What I want of course is a constructor that can be invoke the way I would invoke it if it were an ordinary function:
Scheduled(40, println("x") )
Give baby his bottle!
© Stack Overflow or respective owner