Created
August 4, 2016 16:54
-
-
Save ellbur/f53d301559188d84ec415c310a3fdc04 to your computer and use it in GitHub Desktop.
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
import scala.language.higherKinds | |
object DependentStorageTest extends App { | |
def decidableEquality[X <: AnyRef, Y <: AnyRef](x: X)(y: Y): Option[x.type =:= y.type] = | |
if (x eq y) | |
Some(=:=.tpEquals[x.type].asInstanceOf[x.type =:= y.type]) | |
else | |
None | |
def consistency[X<:U,Y<:U,U,K[_ <: U]](inner: X =:= Y): K[X] =:= K[Y] = | |
=:=.tpEquals[K[X]].asInstanceOf[K[X] =:= K[Y]] | |
class DependentStorage[K <: AnyRef, V[_ <: K]] { | |
private trait StoredValue { | |
val key: K | |
val value: V[key.type] | |
} | |
private var storedValue: Option[StoredValue] = None | |
def put(_key: K)(_value: V[_key.type]) = storedValue = Some(new StoredValue { | |
val key: _key.type = _key | |
val value = _value | |
}) | |
def get(key: K): Option[V[key.type]] = | |
storedValue match { | |
case None => None | |
case Some(sv) => | |
decidableEquality(sv.key)(key) match { | |
case None => None | |
case Some(equality) => Some(consistency(equality)(sv.value)) | |
} | |
} | |
} | |
trait SingleObject { | |
type T | |
val value: T | |
} | |
type SingleObjectTypeMember[S <: SingleObject] = S#T | |
val storage = new DependentStorage[SingleObject, SingleObjectTypeMember] | |
val k1 = new SingleObject { type T = Int; val value = 3 } | |
storage.put(k1)(6) | |
println(storage.get(k1)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment