views:

158

answers:

2

I have the following Scala class:

class Person(var name : String, var age : Int, var email : String)

I would like to use the Person constructor as a curried function:

def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e)

This works, but is there another way to accomplish this? This approach seems a bit tedious and error-prone. I could imagine something like Function.curried, but then for constructors.

+6  A: 

Hi Chris!

may be so:

val mkPerson = Function.curried((n: String,a:Int,e:String) => new Person (n,a,e))
walla
I see. It's still more complex than I'd hoped for: I was thinking of something more along the lines of Haskell constructors.
Chris Eidhof
+5  A: 

This will work:

def mkPerson = (new Person(_, _, _)).curried
Daniel
+1, but you shouldn't need those type ascriptions. `def mkPerson = (new Person(_, _, _)).curried`
pelotom
Oh, indeed. I thought I needed them because of weird constructor thingies.
Daniel
Just a quick note, if you're sure to call it may as well make it `val mkPerson = ...`. Also, if Person were a case class you could simply use `(Person.apply _).curried`
Geoff Reedy
Thanks a lot. This is indeed a lot more concise.
Chris Eidhof