Skip to content

Commit 4344eee

Browse files
committed
More examples for typeclass encodings
1 parent 7ed8464 commit 4344eee

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

tests/pos/typeclass-encoding3.scala

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
M.Impl[T[_]] = Common { type This = T }
77
...
88
9+
- Common definitions in traits may not see trait parameters, but common definitions
10+
in classes may see class parameters.
11+
912
- An extension trait is a trait that uses This or a trait inheriting an extension trait.
1013
- The kind of `This` is the kind of the extension trait.
1114
- Extension traits of different kinds cannot inherit each other and cannot be mixed using `with`.
@@ -80,11 +83,16 @@ object runtime {
8083
def length: Int
8184
}
8285
86+
trait Cmp[A] {
87+
def isSimilar(x: A): Boolean
88+
common def exact: Boolean
89+
}
90+
8391
trait HasBoundedLength extends HasLength {
8492
common def limit: Int
8593
}
8694
87-
extension trait HasBoundedLengthX extends HasBoundedLength {
95+
trait HasBoundedLengthX extends HasBoundedLength {
8896
common def longest: This
8997
}
9098
@@ -96,12 +104,16 @@ object runtime {
96104
def length = xs.length
97105
}
98106
99-
class C2(xs: Array[Int]) extends C1(xs) with HasBoundedLength {
107+
class C2(xs: Array[Int]) extends C1(xs) with HasBoundedLength with Cmp[Seq[Int]] {
108+
def isSimilar(x: Seq[Int]) = xs.deep == x
100109
common def limit = 100
110+
common def exact = true
101111
}
102112
103-
class CG2[T](xs: Array[Int]) extends CG1[T](xs) with HasBoundedLength {
113+
class CG2[T](xs: Array[Int]) extends CG1[T](xs) with HasBoundedLength with Cmp[Seq[T]] {
114+
def isSimilar(x: Seq[T]) = xs.deep == x
104115
common def limit = 100
116+
common def exact = true
105117
}
106118
107119
final class C3(xs: Array[Int]) extends C2(xs) with HasBoundedLengthX {
@@ -237,6 +249,20 @@ object hasLength {
237249
def length: Int
238250
}
239251

252+
trait Cmp[A] extends TypeClass {
253+
val `common`: HasBoundedLength.Common
254+
import `common`._
255+
def isSimilar(x: A): Boolean
256+
}
257+
258+
object Cmp extends TypeClass.Companion {
259+
trait Common extends TypeClass.Common {
260+
type Instance <: HasBoundedLength
261+
def exact: Boolean
262+
}
263+
}
264+
265+
240266
trait HasBoundedLength extends HasLength with TypeClass {
241267
val `common`: HasBoundedLength.Common
242268
import `common`._
@@ -264,27 +290,31 @@ object hasLength {
264290

265291
class C1(xs: Array[Int]) extends HasLength {
266292
def length = xs.length
293+
def isSimilar(x: Seq[Int]) = xs.deep == x
267294
}
268295

269296
class CG1[T](xs: Array[T]) extends HasLength {
270297
def length = xs.length
298+
def isSimilar(x: Seq[T]) = xs.deep == x
271299
}
272300

273-
class C2(xs: Array[Int]) extends C1(xs) with HasBoundedLength {
301+
class C2(xs: Array[Int]) extends C1(xs) with HasBoundedLength with Cmp[Seq[Int]] {
274302
val `common`: C2Common = C2
275303
import `common`._
276304
}
277-
abstract class C2Common extends HasBoundedLength.Common {
305+
abstract class C2Common extends HasBoundedLength.Common with Cmp.Common{
278306
def limit = 100
307+
def exact = true
279308
}
280309
object C2 extends C2Common with SubtypeInjector[C2]
281310

282-
class CG2[T](xs: Array[T]) extends CG1(xs) with HasBoundedLength {
311+
class CG2[T](xs: Array[T]) extends CG1(xs) with HasBoundedLength with Cmp[Seq[T]] {
283312
val `common`: CG2Common[T] = CG2[T]
284313
import `common`._
285314
}
286-
abstract class CG2Common[T] extends HasBoundedLength.Common {
315+
abstract class CG2Common[T] extends HasBoundedLength.Common with Cmp.Common {
287316
def limit = 100
317+
def exact = true
288318
}
289319
object CG2 {
290320
def apply[T] = new CG2Common[T] with SubtypeInjector[CG2[T]]

0 commit comments

Comments
 (0)