Last active
August 29, 2015 14:05
-
-
Save bvenners/7dbc093d36bd973c993e to your computer and use it in GitHub Desktop.
Adding stricter cons and contain operators to covariant lists via an implicit class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> implicit class BVList[A](xs: List[A]) { | |
| def :=:(x: A): List[A] = 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 | |
res0: List[AnyVal] = List(5.0, 1, 2, 3) | |
scala> 5.0 :=: xs | |
<console>:13: error: type mismatch; | |
found : Double | |
required: Int | |
5.0 :=: xs | |
^ | |
scala> 5 :=: xs | |
res2: List[Int] = List(5, 1, 2, 3) | |
scala> xs contains "bob" | |
res3: Boolean = false | |
scala> xs containz "bob" | |
<console>:13: error: type mismatch; | |
found : String("bob") | |
required: Int | |
xs containz "bob" | |
^ | |
scala> xs containz 3 | |
res5: Boolean = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment