6

Is there any way to do something like this

class Foo (val bar:Any, val baz:Any = magic(bar))

without overloading constructor or making baz mutable?

1 Answer 1

8

You should use different parameter groups:

def test(i: Int)(j: Int = i + 1) = i + j
test(1)()
// Int = 3

class Foo (val bar: Int)(val baz: Int = bar + 1)
new Foo(1)().baz
// Int = 2

For constructors you could also use None for default value of constructor parameter and then define val in class body:

class Foo (val bar: Int, _baz: Option[Int] = None) {
  val baz = _baz.getOrElse(bar + 1)
}
new Foo(1).baz
// Int = 2

Note that for case class you can't use different parameter groups (if you want to get all parameters in unapply) nor class body. The only way is to define an addition apply method in companion object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.