Scala: constructor with two paretneheses -
i'm new scala. following syntax mean?
case class user(val id: long)(val username: string)
i've read currying in scala, explain please how related construction above (if related). thanks.
just partially-applied functions, constructor (which function arguments constructed type) can partially applied, in case:
scala> case class user(val id: long)(val username: string) defined class user scala> val userbuilder = user(123l) _ userbuilder: string => user = <function1>
note type of resulting userbuilder
- it's function string
(the remaining parameter) user
now, partially applied functions, can apply result string , user
instance:
scala> val user = userbuilder("a") user: user = user(123) scala> user.username res1: string =
when useful?
when want construct many instances common values subset of arguments, e.g.:
case class person(lastname: string)(val firstname: string) class family(lastname: string, firstnames: list[string]) { def getmembers: list[person] = { val creator = person(lastname) _ // reused each first name! firstnames.map(creator) } }
when want use 1 argument default value of one:
case class measure(min: int)(val max: int = min*2) measure(5)() // max = 10 measure(5)(12) // default overridden, max = 12
when want use
implicit
arguments, must reside in separate, last argument list of function, described int scala language specification (chapter 7.2):
a method or constructor can have 1 implicit parameter list, , must last parameter list given.
Comments
Post a Comment