You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The object `MyTable` inherits its `get` and `set` method from `SynchronizedTable`.
744
743
The `super` calls in these methods are re-bound to refer to the corresponding implementations in `ListTable`, which is the actual supertype of `SynchronizedTable` in `MyTable`.
745
744
745
+
### Extending parameterized traits
746
+
747
+
Extra rules apply for extending a trait with parameters:
748
+
749
+
1. If a class `´C´` extends a parameterized trait `´T´`, and its superclass does not, `´C´`_must_ pass arguments to `´T´`.
750
+
751
+
2. If a class `´C´` extends a parameterized trait `´T´`, and its superclass does as well, `´C´`_must not_ pass arguments to `´T´`.
752
+
753
+
3. Traits must never pass arguments to parent traits.
754
+
755
+
4. If a class `´C´` extends an unparameterized trait `´T_i´` and the base types of `´T_i´` include parameterized trait `´T_j´`, and the superclass of `´C´` does not extend `´T_j´`, then `´C´`_must_ also explicitly extend `´T_j´` and pass arguments.
756
+
This rule is relaxed if the missing trait contains only context parameters. In that case the trait reference is implicitly inserted as an additional parent with inferred arguments.
757
+
758
+
###### Example - Preventing ambiguities
759
+
760
+
The following listing tries to extend `Greeting` twice, with different parameters.
761
+
762
+
```scala
763
+
traitGreeting(valname:String):
764
+
defmsg=s"How are you, $name"
765
+
766
+
classCextendsGreeting("Bob")
767
+
768
+
classDextendsC, Greeting("Bill") // error
769
+
770
+
@main defgreet= println(D().msg)
771
+
```
772
+
773
+
Should this program print "Bob" or "Bill"? In fact this program is illegal, because it violates rule 2 above.
774
+
Instead, `D` can extend `Greeting` without passing arguments.
775
+
776
+
###### Example - Overriding
777
+
778
+
Here's a variant of `Greeting` that overrides `msg`:
779
+
```scala
780
+
traitFormalGreetingextendsGreeting:
781
+
overridedefmsg=s"How do you do, $name"
782
+
```
783
+
784
+
Due to rule 4, the following class extending `FormalGreeting` is required to also extend `Greeting` with arguments:
Due to rule 4, `F` is required to also extend `ImpliedGreeting` and pass arguments to it, however note that because `ImpliedGreeting` has only context parameters the extension was added implicitly.
0 commit comments