Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created January 16, 2012 14:24
Show Gist options
  • Save Sciss/1621097 to your computer and use it in GitHub Desktop.
Save Sciss/1621097 to your computer and use it in GitHub Desktop.
Pimped or operator with automatic upper bound
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