I happened to find that it's not allowed to have abstract private fields in a trait, that is,
trait A1 {
//private val a: Int // Not allowed
protected val b: Int // OK
}
And it seems all right to do such thing to an abstract class, if private fields are constructor parameters, that is,
abstract class A2 (private val i: Int) // OK
So I guess that a trait doesn't have constructor parameters, so there's no way to initialize them, therefore no abstract private fields are allowed.
If they are "protected", then a subclass can initialize them using pre-initialized fields. This approach allows subclass to see these fields.
What if I just want to initialize them and hide them afterwards, as in the following example?
object holding {
trait trick {
protected val seed: Int // Can't be private
final def magic: Int = seed + 123
}
trait new_trick extends trick {
def new_magic: Int = magic + 456
def the_seed: Int = seed // [1]
}
def play: new_trick = new { val seed = 1 } with new_trick
def show_seed(t: new_trick): Int = t.the_seed // [2]
}
I don't want anyone to be able to see seed, that is, [2] (and so [1]) shouldn't be allowed. Is there a way for a trait to do that?
As @Randall and @pagoda_5b have pointed out, my question doesn't make much sense. But luckily @Régis and @axel22 have turned it into another interesting question and provided a pattern for solving it.
seedwhile making further sub-traits (and external code) be unable to access the value. Kind of like a protected value (treat like a protected value for the sub-traits, and treat as private for everything lese). As for the meaningfulness, I think the idea is just to fully emulate what can be done through parameters (in classes) as shown in his example.