Skip to content

Commit 3f5f056

Browse files
committed
Fixed 3.7.1 breakage in the givens scripts examples.
Signed-off-by: Dean Wampler <[email protected]>
1 parent a4c553a commit 3f5f056

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/script/scala/progscala3/contexts/typeclass/MonoidTypeClass.scala

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ IntMonoid.unit <+> 2 // 2
1616
// end::usage[]
1717

1818
// tag::numericdefinition[]
19-
given NumericMonoid[T : Numeric]: Monoid[T]:
19+
// 2025-06-16: The following line was the original syntax in Scala 3
20+
// given NumericMonoid[T : Numeric]: Monoid[T]:
21+
// This is the currently acceptable syntax:
22+
given NumericMonoid: [T: Numeric] => Monoid[T]:
2023
def unit: T = summon[Numeric[T]].zero
2124
extension (t: T)
2225
infix def combine(other: T): T = summon[Numeric[T]].plus(t, other)
@@ -30,25 +33,6 @@ NumericMonoid[BigDecimal].unit <+> BigDecimal(3.14)
3033
NumericMonoid[BigDecimal].unit combine BigDecimal(3.14)
3134
// end::numericdefinition[]
3235

33-
// tag::numericdefinition2[]
34-
given NumericMonoid[T](using num: Numeric[T]): Monoid[T]:
35-
def unit: T = num.zero
36-
extension (t: T)
37-
infix def combine(other: T): T = num.plus(t, other)
38-
// end::numericdefinition2[]
39-
40-
// tag::numericdefinition3[]
41-
given [T : Numeric]: Monoid[T]:
42-
def unit: T = summon[Numeric[T]].zero
43-
extension (t: T)
44-
infix def combine(other: T): T = summon[Numeric[T]].plus(t, other)
45-
// or
46-
given [T](using num: Numeric[T]): Monoid[T]:
47-
def unit: T = summon[Numeric[T]].zero
48-
extension (t: T)
49-
infix def combine(other: T): T = summon[Numeric[T]].plus(t, other)
50-
51-
BigDecimal(3.14) <+> summon[Monoid[BigDecimal]].unit
52-
summon[Monoid[BigDecimal]].unit <+> BigDecimal(3.14)
53-
summon[Monoid[BigDecimal]].unit combine BigDecimal(3.14)
54-
// end::numericdefinition3[]
36+
// See MonoidTypeClass2.scala for an alternative definition.
37+
// We can't write it in this file because loading the script causes errors
38+
// with alternative definitions in scope.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// tag::usage[]
2+
// src/script/scala/progscala3/contexts/typeclass/MonoidTypeClass2.scala
3+
import progscala3.contexts.typeclass.{Monoid, given} // <1>
4+
5+
// 2025-06-16: This file was split off from MonoidTypeClass.scala to
6+
// to eliminate errors that occur with the effectively duplicate definitions
7+
// of monoids for Numerics. Here, we use an anonymous given, rather than the
8+
// named given in the other file. Note the use of summon below.
9+
10+
// tag::numericdefinition3[]
11+
given [T : Numeric] => Monoid[T]:
12+
def unit: T = summon[Numeric[T]].zero
13+
extension (t: T)
14+
infix def combine(other: T): T = summon[Numeric[T]].plus(t, other)
15+
// or
16+
// 2025-06-16: This alternative syntax no longer works. A replacement is TBD.
17+
// given [T](using num: Numeric[T]): Monoid[T]:
18+
// def unit: T = summon[Numeric[T]].zero
19+
// extension (t: T)
20+
// infix def combine(other: T): T = summon[Numeric[T]].plus(t, other)
21+
22+
BigDecimal(3.14) <+> summon[Monoid[BigDecimal]].unit
23+
summon[Monoid[BigDecimal]].unit <+> BigDecimal(3.14)
24+
summon[Monoid[BigDecimal]].unit combine BigDecimal(3.14)
25+
// end::numericdefinition3[]

0 commit comments

Comments
 (0)