-
-
Save jarhart/3344846 to your computer and use it in GitHub Desktop.
type matching test
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
object Holder { | |
var companions = Set[AnyRef]() | |
def companionFor[M](m:M) = { | |
companions.find { | |
case found:M => true | |
case _ => false | |
} | |
} | |
} | |
abstract class Model[M <: Model] { | |
self: M => | |
def test = { | |
Holder.companionFor(self) | |
} | |
} | |
abstract trait CompanionTrait[M <: Model] { self:Model => | |
Holder.companions = Holder.companions ++ Set(self) | |
} | |
class T1 extends Model[T1] { | |
} | |
object T1 extends T1 with CompanionTrait[T1] { | |
} | |
class T2 extends Model[T2] { | |
} | |
object T2 extends T2 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