scala - trait composition will be ignored on new instance if it is already composed in the class level -
here code:
trait foo { def get(x: int): int } trait simple extends foo { override def get(x: int): int = x } trait add15 extends foo { abstract override def get(x: int): int = x + 15 } trait add30 extends foo { abstract override def get(x: int): int = { super.get(x) + 30 } } class queue extends simple add15 add30 new queue add30 0 // same new queue 0, because add30 ignored
i wonder improve have compilation error instead of ignoring ?
class queue extends simple add15 add30 add30
have compilation error
many in advance
the with
not ignored.
the spec new t
actually says new queue add30
equivalent to:
{ class extends queue add30 ; new }
sure, compile that?
in fact:
scala> new queue add30 res8: queue add30 = $anon$1@aa21042 scala> :javap -prv - binary file res8 contains $line18.$read$$iw$$iw$ [snip] private final $line12.$read$$iw$$iw$queue res8; flags: acc_private, acc_final [snip] public $line18.$read$$iw$$iw$(); flags: acc_public code: stack=3, locals=1, args_size=1 0: aload_0 1: invokespecial #19 // method java/lang/object."<init>":()v 4: aload_0 5: putstatic #21 // field module$:l$line18/$read$$iw$$iw$; 8: aload_0 9: new #23 // class $line18/$read$$iw$$iw$$anon$1 12: dup 13: invokespecial #24 // method $line18/$read$$iw$$iw$$anon$1."<init>":()v 16: putfield #17 // field res8:l$line12/$read$$iw$$iw$queue; 19: homecoming
so resulting value queue
, you're instantiating anonymous subclass trivially extends it. i'm lazy (i mean busy) seek -optimize
.
you can turn question around , inquire why with t t
complain trait t inherited twice
? can't linearization handle redundancy?
i think sec case, a
disappears linearization, case:
scala> class x defined class x scala> trait y extends x defined trait y scala> new x y res15: x y = $anon$1@5af97169
where you're mixing in y
extends x
.
but must utilize napkin (serviette) on scribbling primary utilize case , go doing before.
scala
No comments:
Post a Comment