abstract class A {
type ThisType <: A
def assign(other: ThisType): ThisType
}
class B extends A {
override type ThisType = B
override def assign(other: B): B = ???
}
可能声明p1和p2的地方。从本质上讲,他们可以在任何地方宣布。我想这将涵盖大多数使用场景:
1a)的
implicit val p1: /* ??? */
implicit val p2: /* ??? */
implicit val f = { p1, p2 => p1.assign(p2) }
val c: => Unit = { p1, p2 => p1.assign(p2) }
object R1a {
apply(implicit p1: B, p2: B, f: (B, B) => B) = new R1a(p1, p2, f)
}
val f: (B, B) => B = { p1, p2 => p1.assign(p2) }
class R1a(val p1: B, val p2: B, val f: (B, B) => B) extends Runnable {
override def run: Unit = p1.assign(p2)
// and val f: override def run: Unit = f(p1, p2)
}
1b)的
implicit val p1: /* ??? */
implicit val p2: /* ??? */
implicit val f = { p1, p2 => p1.assign(p2) }
val c: => Unit = { p1, p2 => p1.assign(p2) }
object R1b {
apply[T <: A](implicit p1: T, p2: T, f: (T, T) => T) = new R1a(p1, p2, f)
}
class R1b[T <: A](val p1: /* ??? */, val p2: /* ??? */, val f: (/* ??? */, /* ??? */T) => /* ??? */) extends Runnable {
override def run: Unit = p1.assign(p2)
// and val f: override def run: Unit = f(p1, p2)
}
2a)的
class R2a extends Runnable {
type T /* ??? */
val p1: T = /* ??? */
val p2: T = /* ??? */
val f: (T, T) => T = { p1, p2 => p1.assign(p2) }
override def run: Unit = p1.assign(p2)
// and val f: override def run: Unit = f(p1, p2)
}
2b)的
class R2b[T <: A] extends Runnable {
val p1: /* ??? */
val p2: /* ??? */
val f: (T, T) => T = { p1, p2 => p1.assign(p2) }
override def run: Unit = p1.assign(p2)
// and val f: override def run: Unit = f(p1, p2)
}
3a)的
class R3a extends Runnable {
override def run: Unit = {
type T /* ??? */
val p1: T = /* ??? */
val p2: T = /* ??? */
val f: (T, T) => T = { p1, p2 => p1.assign(p2) }
p1.assign(p2)
// and f(p1, p2)
}
}
3b)的
class R3b[T <: A] extends Runnable {
override def run: Unit = {
val p1: /* ??? */ = /* ??? */
val p2: /* ??? */ = /* ??? */
val f: (T, T) => T = { p1, p2 => p1.assign(p2) }
p1.assign(p2)
// and f(p1, p2)
}
}
/* ??? */
表示占位符。无需涵盖所有场景,只要有解释即可。我试图在DSL中使用它,因此有时我不想引入新的类型参数,并且当它们适合DSL设计时也使用它们。
这是一个跟进问题:How do I make sure a function receives the same parameter type as the current object?
如果我必须在run
函数中执行函数赋值,我该如何实现。