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
This is what I found out by experimenting, I did not look into the compiler to see what is actually done (which can be a good or bad thing)
Here is what I could come up with:
https://scastie.scala-lang.org/9rRQTsaAR9q5BJY5Q8LpfQ
We can see there is a strong correlation between `new T{}` and `extends T` being allowed or disallowed.
I found it surprising that `new Obj2{}` is valid, while `new Obj{}` is not, given they are both valid with `new ...()(){}`, should I open an issue for that ?
Copy file name to clipboardExpand all lines: docs/_docs/reference/other-new-features/trait-parameters.md
+39-6Lines changed: 39 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -53,13 +53,19 @@ The correct way to write `E` is to extend both `Greeting` and
53
53
classEextendsGreeting("Bob"), FormalGreeting
54
54
```
55
55
56
-
## Traits With Context Parameters
56
+
## Default values and implicit parameters
57
57
58
-
This "explicit extension required" rule is relaxed if the missing trait contains only
59
-
[context parameters](../contextual/using-clauses.md). In that case the trait reference is
60
-
implicitly inserted as an additional parent with inferred arguments. For instance,
61
-
here's a variant of greetings where the addressee is a context parameter of type
62
-
`ImpliedName`:
58
+
If the trait `T` is parametrised such that `new T{}` would be a legal annonymous class creation, the "explicit extension required" rule does not apply, the parameters filled in as for the annonymous class:
59
+
* Parameters with default values are set to their default value.
60
+
*[Context parameters](../contextual/using-clauses.md) are set to the value present in the implicit scope. Notably from class parameters or other traits.
61
+
62
+
```scala
63
+
traitwithLastName(vallastName:String="")
64
+
65
+
caseclassPersonextends withLastName
66
+
// identical to
67
+
caseclassPersonextends withLastName("")
68
+
```
63
69
64
70
```scala
65
71
caseclassImpliedName(name: String):
@@ -83,6 +89,33 @@ class F(using iname: ImpliedName) extends
83
89
```
84
90
Note the inserted reference to the super trait `ImpliedGreeting`, which was not mentioned explicitly.
85
91
92
+
In the above context, the following is also valid:
0 commit comments