Created
January 18, 2013 01:40
-
-
Save johnynek/4561592 to your computer and use it in GitHub Desktop.
ds
This file contains hidden or 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
abstract class Marker(val g: AnyRef) | |
// Codegen 100 - 1000 of these, or use ASM to generate them on the fly | |
class Marker1(g: AnyRef) extends Marker(g) | |
class Marker2(g: AnyRef) extends Marker(g) | |
type Fn = (AnyRef) => Marker | |
class Markers(markers: List[(Fn, Class[_])], mmap: Map[Manifest[T], (Fn, Class[_])] = Map.empty) { | |
def get[T](implicit mf: Manifest[T]): (Fn, Class[_], Markers) = { | |
mmap.get(mf) { case (fn,cls) => | |
(fn, cls, this) | |
} | |
.getOrElse { | |
(markers.head._1, markers.head._2, new Markers(markers.tail, mmap + (mf -> fn))) | |
} | |
} | |
} | |
var markers: Markers = new Markers(List((fn1, classOf[Marker1]), (fn2, classOf[Marker2], (fn3, classOf[Marker3], ...)) | |
val mutex = new Object | |
class MarkerRecord[T](val fn: Fn, cls: Class[_]) | |
implicit def getMarker[T:Manifest]: MarkerRecord[T] = { | |
mutuex.synchronized { | |
val (fn, cls, markers) = markers.get[T] | |
new MarkerRecord[T](fn, cls) | |
} | |
} | |
// Now do: implicitly[MarkerRecord[T]] and you will get the unique value for this type T. | |
// This prevents type erasure from fucking up Kryo. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment