Scala:不变性和依赖路径的类型兼容性 [英] Scala: immutability and path-dependent type compatibility
问题描述
我已经就这个话题提出了一些问题,但这次我想进行更笼统的讨论,因为在我看来 Scala 缺少一些非常重要的块.
I have asked a few questions around this topic but this time I want to make it a more general discussion, since it seems to me that Scala is lacking some very important blocks.
考虑以下代码(从我的真实项目中简化),
Consider the following code (which is simplified from my real project),
trait World {
type State <: StateIntf
def evolve(s: State): State
def initialState: State
}
class Algorithm(world: World) {
def process(s: world.State) {
val s1 = world.evolve(s)
// ... do something with s and s1
}
}
一切看起来都是那么美好和数学化,但是
Everything seems so beautiful and mathematical, but
object SomeWorld extends World {...}
new Algorithm(SomeWorld).process(SomeWorld.initialState) // incompatible type
当然可以通过以下方式进行
Certainly you can do in the following way
trait World {
type State <: StateIntf
var s: State
def evolve: Unit // s = next state
def initialize: Unit // s = initial state
def getState: StateIntf = s
}
但我们又回到了可变的世界.
But we are just back to the mutable world.
我被告知这是因为 Scala 没有流分析.如果那是问题所在,Scala 不应该得到那一块吗?我只需要编译器知道从 val
传递到 val
的值是相同的,因此它们的内部类型必须一致.这对我来说似乎很自然,因为:
I am told that this is because Scala does not have flow analysis. If that is the problem, shouldn't Scala get that piece? I only need that compilor can be aware that values passed from val
to val
are the same so that their inner types must agree. This seems so natural to me, as:
val
是涉及 Scala 中不变性的最基本概念- 需要路径相关的类型兼容性来对
World
之类的事物进行建模,并具有完全不变性(从数学角度来看这是非常需要的) - 传递
val
的流程分析解决问题
val
is the most foundamental concept that involves immutability in scala- Path dependent type compatability is needed to model things like
World
with complete immutability (which is highly desired from a mathematical perspective) - Flow analysis of passing
val
s solve the problem
我要求太多了吗?或者已经有很好的解决方法?
Am I asking for too much? Or is there already a nice way of solving it?
推荐答案
编译器有时需要一点帮助来证明您在使用路径依赖类型时所做的事情是合法的.即,正如您所说,编译器缺少流程分析,因此我们必须明确告诉它我们不只是使用任何 World
,我们正在使用的是 SomeWorld
,以便我们可以使用 SomeWorld.initialState
.
The compiler sometimes needs a little bit of help to prove that what you are doing is legal when using path dependent types. I.e., as you said, the compiler is missing flow analysis, so we must tell it explicitly that we aren't just using any World
, we are using exactly SomeWorld
so that we can use SomeWorld.initialState
.
在您的情况下,如果您像这样更改 Algorithm
:
In your case, if you change Algorithm
like so:
class Algorithm[W <: World](world: W) {
def process(s: world.State) {
val s1 = world.evolve(s)
// ... do something with s and s1
}
}
然后编译如下:
object SomeWorld extends World {...}
new Algorithm[SomeWorld.type](SomeWorld).process(SomeWorld.initialState)
这篇关于Scala:不变性和依赖路径的类型兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!