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?
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.