5 ms·
Scala/Haskell has the same thing, and it's an amazing feature. The proper type is that of trait IndexedStateT[A, B, C] Which signifies a typelevel state m
by dhash 7y ago
Scala/Haskell has the same thing, and it's an amazing feature. The proper type is that of
trait IndexedStateT[A, B, C]
Which signifies a typelevel state machine moving from state A to state B emitting a value of type C.
I can only speak for scala, but i'm assuming haskell has singleton and literal types as well. Meaning that code like this works great.
object DoorOpen
object DoorClosed
class Door {
def open: IndexedState[DoorClosed.type, DoorOpen.type, Unit]
def close: IndexedState[DoorOpen.type, DoorClosed.type, Unit]
}
val d = Door()
for {
_ <- d.open() //works
_ <- d.close() // works
// _ <- d.close() //compile error
}
By my understanding of the article, it uses the borrow/move state to implement the state transistion. Is this generalizable to arbitrary state machines, or only a simple 2-state one?
- Rusky 7y agoIt generalizes to arbitrary state machines- check out the reference near the end to session types. Each state is a type, methods consume their receiver (the "move state") and return the new state. You can't accidentally keep around a copy of the old state.