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`.
746
745
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`.
747
746
747
+
### Extending parameterized traits
748
+
749
+
Extra rules apply for extending a trait with parameters:
750
+
751
+
1. If a class `´C´` extends a parameterized trait `´T´`, and its superclass does not, `´C´`_must_ pass arguments to `´T´`.
752
+
753
+
2. If a class `´C´` extends a parameterized trait `´T´`, and its superclass does as well, `´C´`_must not_ pass arguments to `´T´`.
754
+
755
+
3. Traits must never pass arguments to parent traits.
756
+
757
+
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.
758
+
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.
759
+
760
+
###### Example - Preventing ambiguities
761
+
762
+
The following listing tries to extend `Greeting` twice, with different parameters.
763
+
764
+
```scala
765
+
traitGreeting(valname:String):
766
+
defmsg=s"How are you, $name"
767
+
768
+
classCextendsGreeting("Bob")
769
+
770
+
classDextendsC, Greeting("Bill") // error
771
+
772
+
@main defgreet= println(D().msg)
773
+
```
774
+
775
+
Should this program print "Bob" or "Bill"? In fact this program is illegal, because it violates rule 2 above.
776
+
Instead, `D` can extend `Greeting` without passing arguments.
777
+
778
+
###### Example - Overriding
779
+
780
+
Here's a variant of `Greeting` that overrides `msg`:
781
+
```scala
782
+
traitFormalGreetingextendsGreeting:
783
+
overridedefmsg=s"How do you do, $name"
784
+
```
785
+
786
+
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