0

I'm trying to create a wrapper function for system.scheduler.schedule. I started by writing the following code:

def every(time : Int) : Unit = {

    val system = ActorSystem("timer")
    import system.dispatcher
    val tickActor = system.actorOf(Props(classOf[TickActor], this))

    system.scheduler.schedule(0 milliseconds, time.milliseconds, tickActor, Tick)
}

I'm trying to use it like this:

timer.every(10) {
    println("HI")
}

However, I'm getting: Unit does not take parameters

I don't think I've set my function call up correctly to accept the { }, but I don't know how. I don't even know the terminology so I can look it up... I'm really knew to scala.

What do they call something like this and what do I need to do to fix it?

Stradigos
  • 814
  • 1
  • 13
  • 29
  • 3
    you can write something like `def every(time:Int)(thunk: => Unit){...}` http://stackoverflow.com/questions/22670356/scala-passing-function-as-block-of-code-between-curly-braces – ymonad Jun 13 '16 at 01:24
  • Brilliant, thank you. – Stradigos Jun 13 '16 at 02:14
  • From Martin Odersky, Programming in Scala, 2nd Edtition : ``` In any method invocation in Scala in which you’re passing in exactly one argument, you can opt to use curly braces to surround the argument instead of parentheses.``` Chapter 9, Control Abstraction, p 216 – Louis F. Jun 13 '16 at 10:06

0 Answers0