Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Scala constructor with multiple argument lists

In Scala, I can create a method that takes more than one argument list:

def myMethod(value: Int)(fn: (Int) => Unit) {
  fn(value)
}

and call it like this:

myMethod(10) { value => println(value) }

How can I do the same thing with a class constructor? How do I call it?

like image 310
Ralph Avatar asked Feb 03 '26 13:02

Ralph


1 Answers

This works.

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class MyClass(val value: Int)(val fn: Int => Unit)
defined class MyClass

scala> new MyClass(10)({value => println(value)})
res0: MyClass = MyClass@17577f9
like image 76
missingfaktor Avatar answered Feb 06 '26 03:02

missingfaktor