I'm not arguing that there aren't possible reasonable distinction between "no value" and "empty value". I just think that in many cases, it's completely opaque to the user of an API what the two cases actually mean semantically and it would be better to express the difference more clearly through the type system, e.g. by using an enum. (I actually think the Bool?
example is even worse, what does it mean that a method can return true, false or nilâespecially if the method/property is called isSomething
or hasSomething
)? This doesn't tell me anything, having an enum with three cases would be much clearer.)
I don't know anything about app development, so I might be wrong here, but isn't there a difference between a label and its content? E.g. I would rather write this as
struct Label {
let value: String
}
struct SomeView {
let label: Label?
}
Then you don't need "optional string", just optional label containing string.