Skip to content

Commit 9238565

Browse files
Add new givens example
1 parent 59282e6 commit 9238565

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

blog/_posts/2024-08-21-scala-3.5.0-released.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,39 @@ point match
9797

9898
This is an implementation of [SIP-58](https://github.com/scala/improvement-proposals/blob/d649f6e6f333cd9232d85a12bd0445d18a673f10/content/named-tuples.md).
9999

100+
### Experimental: new givens and context bounds syntax
101+
102+
Another experimental feature introduced in Scala 3.5 is the new syntax for type classes. Some of these improvements are: `Self` type member instead of the type parameter, auxiliary type alias `is` or named context bounds, just to name a few.
103+
The full list of proposed improvements and their examples can be found under [Modularity Improvements](https://scala-lang.org/api/3.5.0/docs/docs/reference/experimental/modularity.html) and [Better Support for Type Classes](https://scala-lang.org/api/3.5.0/docs/docs/reference/experimental/typeclasses.html).
104+
To test the new syntax you would be required to use both the source version `future` and the additional language import `experimental.modularity`.
105+
106+
```scala
107+
//> using options -source:future -language:experimental.modularity
108+
109+
trait Ord:
110+
type Self
111+
extension (x: Self)
112+
def compareTo(y: Self): Int
113+
def < (y: Self): Boolean = compareTo(y) < 0
114+
def > (y: Self): Boolean = compareTo(y) > 0
115+
116+
given intOrd: (Int is Ord) with
117+
extension (x: Int) def compareTo(y: Int) = if x < y then -1 else if x > y then +1 else 0
118+
119+
def descending[T: Ord as asc]: T is Ord = new:
120+
extension (x: T) def compareTo(y: T) = asc.compareTo(y)(x)
121+
122+
def maximum[T](xs: List[T])(using T is Ord): T =
123+
xs.reduceLeft((x, y) => if (x < y) y else x)
124+
125+
val xs = List(1, 2, 3)
126+
val _ = maximum(xs)
127+
val _ = maximum(xs)(using descending)
128+
val _ = maximum(xs)(using descending(using intOrd))
129+
```
130+
131+
This is an implementation of [SIP-64](https://github.com/scala/improvement-proposals/blob/db5cc6ab92758272f1a3528eacb46182ea216323/content/typeclasses-syntax.md).
132+
100133
## Work on a better scheme for given prioritization
101134

102135
Givens in Scala 3 have a peculiar problem with prioritization. The compiler tries to always select the instance with *the most specific subtype* of the requested type. This can lead to confusing situations, when user faces ambiguity errors in code that should intuitively work. Changing the scheme of given prioritization to always select the instance with *the most general subtype* that satisfies the context bound, would resolve such cases. We have conducted experiments that showed that the proposed scheme will result in a more intuitive and predictable given resolution. The negative impact on the existing projects is very small. We have tested 1500 open-source libraries, and new rules are causing problems for less than a dozen of them. We have already submitted PRs with changes that will make them work the same way under both the current and proposed rules.

0 commit comments

Comments
 (0)