Created
January 16, 2012 14:24
-
-
Save Sciss/1621097 to your computer and use it in GitHub Desktop.
Pimped or operator with automatic upper bound
This file contains hidden or 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
trait Ev[ A ] { def pull: Option[ A ]} | |
class EvOr[ A, A1 <: A, A2 <: A ]( val a: Ev[ A1 ], val b: Ev[ A2 ]) extends Ev[ A ] { | |
def pull = a.pull.orElse( b.pull ) | |
} | |
class EvOps[ A ]( ev: Ev[ A ]) { | |
def |[ AU >: A, A1 <: AU ]( that: Ev[ A1 ]) = new EvOr[ AU, A, A1 ]( ev, that ) | |
} | |
implicit def evOps[ A ]( ev: Ev[ A ]) = new EvOps[ A ]( ev ) | |
case class EvImp[ A ]( value: A ) extends Ev[ A ] { def pull = Some( value )} | |
val a = EvImp( List( 1, 2 )) | |
val b = EvImp( Set( 3, 4 )) | |
val c = a | b | |
sealed trait Update { def value: Int }; case class U1( value: Int ) extends Update; case class U2( value: Int ) extends Update | |
val or = EvImp( U1( 3 )) | EvImp( U2( 4 )) | |
or.pull |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment