aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/Monoid.scala
blob: f6004cdf319f4b16468bd40a5671d7316564c6f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package strawman.typeclasses

trait SemiGroup[T] {
  def append(x: T, y: T): T
}
object SemiGroup {
  class Ops {
    implicit class InfixAppend[T: SemiGroup](self: T) {
      def |+| (other: T): T = implicitly[SemiGroup[T]].append(self, other)
    }
  }
  object ops extends Ops
}

trait Monoid[T] extends SemiGroup[T] {
  val id: T
}
object Monoid {
  object ops extends SemiGroup.Ops
}

trait Ring[T] extends Monoid[T] {
  val zero = id
  val one: T
  def product(x: T, y: T): T
}
object Ring {
  class Ops extends SemiGroup.Ops {
    implicit class InfixProduct[T: Ring](self: T) {
      def |*| (other: T): T = implicitly[Ring[T]].product(self, other)
    }
  }
  object ops extends Ops
}



object Test {
  implicit object StringMonoid extends Monoid[String] {
    def append(x: String, y: String): String = x ++ y
    val id = ""
  }

  implicit object IntRing extends Ring[Int] {
    def append(x: Int, y: Int) = x + y
    val id = 0
    val one = 1
    def product(x: Int, y: Int) = x * y
  }

  import Monoid.ops._ // works in dotty, fails in scalac
  import Ring.ops._
  "abc" |+| "def"
  "abc" |+| StringMonoid.id
  StringMonoid.id |+| "abc"

  1 |+| 2
  3 |*| 3


}