Skip to content

Instantly share code, notes, and snippets.

@matthandlersux
Created August 13, 2012 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthandlersux/3344752 to your computer and use it in GitHub Desktop.
Save matthandlersux/3344752 to your computer and use it in GitHub Desktop.
type matching test
object Holder {
var companions = Set[AnyRef]()
def companionFor[M](m:M) = {
companions.find {
case found:M => true
case _ => false
}
}
}
abstract class Model {
def test = {
Holder.companionFor(this)
}
}
abstract trait CompanionTrait[M <: Model] { self:Model =>
Holder.companions = Holder.companions ++ Set(self)
}
class T1 extends Model {
}
object T1 extends Model with CompanionTrait[T1] {
}
class T2 extends Model {
}
object T2 extends Model with CompanionTrait[T2] {
}
T1
T2
val x = new T1
val y = new T2
println("testing")
assert(x.test == Some(T1))
println("passed 1")
assert(y.test != Some(T1))
println("passed 2")
assert(y.test == Some(T2))
println("passed 3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment