Skip to content

Instantly share code, notes, and snippets.

@bvenners
Created August 18, 2014 19:02
Show Gist options
  • Save bvenners/97023424974b08adcafb to your computer and use it in GitHub Desktop.
Save bvenners/97023424974b08adcafb to your computer and use it in GitHub Desktop.
Adding stricter cons and contain operators to covariant lists via an implicit class, take 2
scala> implicit class BVList[A](xs: List[A]) {
| def :=:[B >: A](x: B)(implicit ev: B =:= A): List[B] = x :: xs
| def containz(elem: A): Boolean = xs.contains(elem)
| }
defined class BVList
scala> val xs = List(1, 2, 3)
xs: List[Int] = List(1, 2, 3)
scala> 5.0 :: xs
res4: List[AnyVal] = List(5.0, 1, 2, 3)
scala> 5.0 :=: xs
<console>:19: error: Cannot prove that AnyVal =:= Int.
5.0 :=: xs
^
scala> 5 :=: xs
res6: List[Int] = List(5, 1, 2, 3)
scala> xs contains "bob"
res7: Boolean = false
scala> xs containz "bob"
<console>:19: error: type mismatch;
found : String("bob")
required: Int
xs containz "bob"
^
scala> xs containz 3
res9: Boolean = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment