Created
August 13, 2012 23:18
-
-
Save matthandlersux/3344752 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 { | |
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