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.
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
is the most foundamental concept that involves immutability in scalaWorld
with complete immutability (which is highly desired from a mathematical perspective)val
s solve the problemAm I asking for too much? Or is there already a nice way of solving it?
I think that generics provides a simpler solution to this problem:
trait World[S <: StateInf] {
def evolve(s: S): S
def initialState: S
}
class Algorithm[S <: StateInf](world: World[S]) {
def process(s: S) {
val s1 = world.evolve(s)
// ... do something with s and s1
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With