ssanj
ssanj

Reputation: 2179

Access first default parameter value while supplying the rest

Given:

case class Person(name:String = "Bob", age:Int = 20)

How can I accept the default value for name while supplying the age?

Eg. I can do this:

Person() -> Person("Bob", 20)

Person("Jim") -> Person("Jim", 20)

How can I do this:

Person(,35) -> Person("Bob", 35)

Upvotes: 1

Views: 207

Answers (1)

Moritz
Moritz

Reputation: 14202

You can use named parameters with default values:

case class Person(name: String = "Bob", age: Int = 20)

Person(age = 23)

Upvotes: 6

Related Questions