Skip to content

Instantly share code, notes, and snippets.

@hstraub
Created March 6, 2015 13:47
Show Gist options
  • Save hstraub/da1add4573520c5c1bfa to your computer and use it in GitHub Desktop.
Save hstraub/da1add4573520c5c1bfa to your computer and use it in GitHub Desktop.
Scala trait with type
trait Super {
type A
def value: A
}
case class B1( value: String ) extends Super {
type A = String
}
case class B2( value: Float ) extends Super {
type A = Float
}
// implicit def impConv( x: B2 ): B1 = B1( x.value.toString )
object Func {
def f1( base: Super ) = {
base match {
case x: B1 => println( "B1: " + x.value )
case x: B2 => println( "B2: " + x.value )
case _ => throw new Exception( "Implementation error." )
}
}
def f2( base: Super ) = {
println( base.value )
}
def z1( base: B1 ) {
println( "B1: " + base.value )
}
def z1( base: B2 ) {
println( "B2: " + base.value )
}
def test( base: B2 ): B1 = {
B1( base.value.toString )
}
def impl( base: B1 ) {
z1( base )
}
}
val b1 = B1( "test" )
val b2 = B2( 2f )
Func.f2( b1 )
Func.f2( b2 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment