-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement trait parameters (SIP) #640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Verify that the initilialization order described in scala#640 is correctly implemented.
Verify that the initilialization order described in scala#640 is correctly implemented.
Verify that the initilialization order described in scala#640 is correctly implemented.
Bread crumbs for future documenters:
|
Current implementation can be circumvented by anonymous class:
Is it expected? |
@texasbruce looks like the override is actually the problem |
@SethTisue, to me it looks like the main difference is that traits can't have secondary constructors, cf. #14184. |
Classes are the spine where everything else is initialized. You need a class, not a trait, to pass arguments to a super-trait. |
We would like to allow parameters to traits. These replace early definitions, which are complicated and hard to get right. The syntax already allows this. Excerpting from current SyntaxSummary.txt (the one for Scala 2 is analogous):
Parent traits can now be introduced as a type or as a constructor which can take arguments. The order of initialization of traits is unaffected by parameter passing - as always, traits are initialized in linearization order. The following rules ensure that every parameterized trait is passed an argument list exactly when it is initialized:
C
implements a parameterized traitT
, and its superclass does not, thenT
must appear as a parent trait ofC
with arguments. By contrast, if the superclass ofC
also implementsT
, thenC
may not pass arguments toT
.For example, assume the declarations
U
may not pass arguments toT
. On the other hand, a class implementingU
must ensure thatT
obtains arguments for its parameters. So the following would be illegal:We have to add the trait
T
as a direct parent ofC
. This can be done in one of two ways:Both class definitions have the same linearization.
T
is in each case initialized beforeU
sinceT
is inherited byU
.The arguments to a trait are in each case evaluated immediately before the trait initializer is run (except for call-by-name arguments, which are always evaluated on demand).
This means that in the example above the expression
e
is evaluated before the initializer of eitherT
orU
is run. On the other hand, assuming the declarationsthe evaluation order would be
e1
, initializer ofT
,e2
, initializer ofV
.The text was updated successfully, but these errors were encountered: