通过将类型参数与参数的路径相关类型进行匹配来约束操作

我想利用Scala的类型系统来约束系统中的操作,在该系统中存在对某些值的版本化引用。这一切都发生在一些事务上下文
Ctx
中,它附带了一个版本类型
V
。现在有一个
Factory
来创建引用变量。它们是通过附加它们的创建版本创建的(类型参数
V1
),对应于调用工厂的上下文版本。 现在想象一些代码试图在更高版本中访问该引用,即使用不同的
Ctx
。我想要实现的是禁止在任何版本(
Ctx
V
类型字段)中调用与该创建版本不匹配的
Ref
访问,但允许您通过某种替换机制解析引用
Ref
的新视图,可以在当前版本中访问。 (如果用无效的上下文调用
substitute
就行了,例如一个比
Ref
的older3ѭ更旧 - 在这种情况下可能会抛出运行时异常) 这是我的尝试:
trait Version

trait Ctx {
  type V <: Version
}

object Ref {
  implicit def access[C <: Ctx, R, T](r: R)(implicit c: C, view: R => Ref[C#V, T]): T =
    view(r).access(c)

  implicit def substitute[C <: Ctx, T](r: Ref[_ <: Version, T])
                                      (implicit c: C): Ref[C#V, T] = r.substitute(c)
}
trait Ref[V1 <: Version, T] {
  def access(implicit c: { type V = V1 }): T // ???
  def substitute[C <: Ctx](implicit c: C): Ref[C#V, T]
}

trait Factory {
  def makeRef[C <: Ctx, T](init: T)(implicit c: C): Ref[C#V, T]
}
而问题是以整个事物编译的方式定义类方法
access
,即复合对象的
access
应该编译,但同时我不能用任何
Ctx
调用这个类方法
access
,只有一个版本匹配的方法参考版本。 优选地,没有结构类型或任何强加性能问题的东西。     
已邀请:
仅供参考,并且关闭这个问题,这是我喜欢的另一个想法,因为客户端代码相当简洁:
trait System[A <: Access[_]] {
  def in[T](v: Version)(fun: A => T): T
}

trait Access[Repr] {
  def version: Version
  def meld[R[_]](v: Version)(fun: Repr => Ref[_, R]): R[this.type]
}

trait Version

trait Ref[A, Repr[_]] {
  def sub[B](b: B): Repr[B]
}

object MyRef {
  def apply[A <: MyAccess](implicit a: A): MyRef[A] = new Impl[A](a)

  private class Impl[A](a: A) extends MyRef[A] {
    def sub[B](b: B) = new Impl[B](b)
    def schnuppi(implicit ev: A <:< MyAccess) = a.gagaism
  }
}
trait MyRef[A] extends Ref[A, MyRef] {
  // this is how we get MyAccess specific functionality
  // in here without getting trapped in more type parameters
  // in all the traits
  def schnuppi(implicit ev: A <:< MyAccess): Int
}

trait MyAccess extends Access[MyAccess] {
  var head: MyRef[this.type]
  var tail: MyRef[this.type]
  def gagaism: Int
}

def test(sys: System[MyAccess], v0: Version, v1: Version): Unit = {
  val v2 = sys.in(v0) { a => a.tail = a.meld(v1)(_.head); a.version }
  val a3 = sys.in(v2) { a => a }
  val (v4, a4) = sys.in(v1) { a =>
    a.head = a.head
    println(a.head.schnuppi) // yes!
    (a.version, a)
  }
  // a3.head = a4.head // forbidden
}
    
以下似乎有效:
trait Version

trait Ctx[+V1 <: Version] {
  type V = V1
}

type AnyCtx   = Ctx[_ <: Version]
type AnyRf[T] = Ref[_ <: Version, T]

object Ref {
  implicit def access[C <: AnyCtx, R, T](r: R)(
    implicit c: C, view: R => Ref[C#V, T]): T = view(r).access(c)

  implicit def substitute[C <: AnyCtx, T](r: AnyRf[T])(implicit c: C): Ref[C#V, T] =
    r.substitute( c )
}
trait Ref[V1 <: Version, T] {
  def access(implicit c: Ctx[V1]): T
  def substitute[C <: AnyCtx](implicit c: C): Ref[C#V, T]
}

trait Factory {
  def makeVar[C <: AnyCtx, T](init: T)(implicit c: C): Ref[C#V, T]
}

// def shouldCompile1(r: AnyRf[String])(implicit c: AnyCtx): String = r

def shouldCompile2(r: AnyRf[String])(implicit c: AnyCtx): String = {
  val r1 = Ref.substitute(r)
  r1.access(c)
}

// def shouldFail(r: AnyRf[String])(implicit c: AnyCtx): String = r.access(c)
所以后续问题是 为什么我需要这种类型的冗余
Ctx
的参数来实现这一点。我讨厌这些类型 参数在我的代码中像兔子一样累积。 为什么
shouldCompile1
不编译 &mdash;我可以获得按计划工作的暗示吗? 编辑: 这也是错的。方差注释是错误的。因为现在以下编译虽然它不应该:
def versionStep(c: AnyCtx): AnyCtx = c // no importa

def shouldFail3[C <: AnyCtx](f: Factory, c: C): String = {
  val r    = f.makeVar("Hallo")(c)
  val c2   = versionStep(c)
  r.access(c2)
}
    

要回复问题请先登录注册