Hello, I wanted to implement some fine pattern of stateful types so the following code was written
protocol StatefulType {
associatedtype State
var currentState: State { get } // lets pretend that it is always enum
}
class File: StatefulType {
public class Opened {
var fileHandle = "some"
}
public class Closed {}
public enum State {
case opened(Opened)
case closed(Closed)
}
private(set) var currentState: State
init() {currentState = .opened(.init())}
}
class SpecFile: File {
enum SpecState {
case opened(SpecOpened)
case closed(SpecOpened)
case somethingDifferent
}
class SpecOpened: Opened {}
class SpecClosed: Closed {}
override var currentState: SpecState
override init() {
currentState = .somethingDifferent
}
}
The fun part is that it cant compile, because compiler complaints about incompatibility of SpecState
and State
. The most obvious solution would be to allow enum cases to be inherited, but I found out that it's not possible. A quick search on forums showed that I was not the only one who encountered the problem , so I want to know from folks around whether it is going anywhere. thank in advance.