File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ object Orderings {
2
+
3
+ // A type class:
4
+ trait Ord [T ] { def less (x : T , y : T ): Boolean }
5
+
6
+ implicit val intOrd : Ord [Int ] = new {
7
+ def less (x : Int , y : Int ) = x < y
8
+ }
9
+
10
+ implicit def listOrd [T ](implicit ev : Ord [T ]): Ord [List [T ]] = new {
11
+ def less (xs : List [T ], ys : List [T ]): Boolean =
12
+ if ys.isEmpty then false
13
+ else if xs.isEmpty then true
14
+ else if xs.head == ys.head then less(xs.tail, ys.tail)
15
+ else ev.less(xs.head, ys.head)
16
+ }
17
+
18
+ def isLess [T ]: T => T => implicit Ord [T ] => Boolean =
19
+ x => y => implicitly[Ord [T ]].less(x, y)
20
+ }
21
+ class Misc {
22
+ class Context
23
+ implicit val ctx : Context
24
+
25
+ f(2 ) // OK
26
+ f(ctx) // error: found: Context, required: Int
27
+ f.explicitly(ctx) // OK
28
+
29
+ }
You can’t perform that action at this time.
0 commit comments