I'm trying to achieve the effect of Kotlin sealed class in Swift, so that I can implement a class-based alternative to enums with associated types.
The following results in a compiler error:
final class Foo {
class Bar: Foo {} // Error: inheritance from a final class "Foo"
}
Is there a way to effectively "seal" a Swift class from further subclassing but still allow subclassing in the first place?
I would give a look at this Using Kotlin’s sealed class to approximate Swift’s enum with associated data and (mostly) this Swift Enumerations Docs
Kotlin:
and then:
While in Swift:
And then do things like:
or: