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
Allow to constrain type variables to be singletons by a context bound
[X: Singleton] instead of an unsound supertype [X <: Singleton]. This
fixes the soundness hole of singletons.
It is intended to turn features described here into proposals under the Scala improvement process. A first installment is SIP 64, which covers some syntactic changes, names for context bounds, multiple context bounds and deferred givens. The order of exposition described in this note is different from the planned proposals of SIPs. This doc is not a guide on how to sequence details, but instead wants to present a vision of what is possible. For instance, we start here with a feature (Self types and `is` syntax) that has turned out to be controversial and that will probably be proposed only late in the sequence of SIPs.
31
+
30
32
## Generalizing Context Bounds
31
33
32
34
The only place in Scala's syntax where the type class pattern is relevant is
@@ -54,6 +56,8 @@ requires that `Ordering` is a trait or class with a single type parameter (which
54
56
55
57
traitMonoidextendsSemiGroup:
56
58
defunit:Self
59
+
objectMonoid:
60
+
defunit[M](usingm: Monoid { typeSelf=M}):M
57
61
58
62
traitFunctor:
59
63
typeSelf[A]
@@ -129,14 +133,17 @@ We introduce a standard type alias `is` in the Scala package or in `Predef`, def
This makes writing instance definitions quite pleasant. Examples:
136
+
This makes writing instance definitions and using clauses quite pleasant. Examples:
133
137
134
138
```scala
135
139
givenInt is Ord ...
136
140
givenInt is Monoid ...
137
141
138
142
typeReader= [X] =>>Env=>X
139
143
givenReader is Monad ...
144
+
145
+
objectMonoid:
146
+
defunit[M](usingm: M is Monoid):M
140
147
```
141
148
142
149
(more examples will follow below)
@@ -682,7 +689,7 @@ With the improvements proposed here, the library can now be expressed quite clea
682
689
683
690
## Suggested Improvements unrelated to Type Classes
684
691
685
-
The following improvements elsewhere would make sense alongside the suggested changes to type classes. But they are currently not part of this proposal or implementation.
692
+
The following two improvements elsewhere would make sense alongside the suggested changes to type classes. But only the first (fixing singleton) forms a part of this proposal and is implemented.
686
693
687
694
### Fixing Singleton
688
695
@@ -704,7 +711,7 @@ Then, instead of using an unsound upper bound we can use a context bound:
704
711
deff[X:Singleton](x: X) = ...
705
712
```
706
713
707
-
The context bound would be treated specially by the compiler so that no using clause is generated at runtime.
714
+
The context bound is treated specially by the compiler so that no using clause is generated at runtime (this is straightforward, using the erased definitions mechanism).
708
715
709
716
_Aside_: This can also lead to a solution how to express precise type variables. We can introduce another special type class `Precise` and use it like this:
0 commit comments