Scala: curried constructors

前端 未结 3 496
猫巷女王i
猫巷女王i 2020-12-17 09:43

I have the following Scala class:

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

I would like to use the Person constru

相关标签:
3条回答
  • 2020-12-17 10:11

    A bit late to this party, but if you make Person a case class:

    scala> case class Person(name: String, age: Int, email: String)
    defined class Person
    

    Scala generates a companion object containing Person.apply(String, Int, String) and some other stuff for you. Then you can do:

    scala> Person.curried
    res5: String => (Int => (String => Person)) = <function1>
    

    Which is shorthand for:

    (Person.apply _).curried
    

    It works with var parameters too.

    0 讨论(0)
  • 2020-12-17 10:19

    may be so:

    val mkPerson = Function.curried((n: String,a:Int,e:String) => new Person (n,a,e))
    0 讨论(0)
  • 2020-12-17 10:24

    This will work:

    def mkPerson = (new Person(_, _, _)).curried
    
    0 讨论(0)
提交回复
热议问题