I'm NOT asking if these extensions are a good idea or not, it's just a thought experiment, I'm trying to learn from the practice.
Agreeing with Christian Lattner, that methods are generally preferable, I thought I'd play with being able to express:
someVariable.isNil
and
someVariable.notNil
Implementing it, I found myself curious if one or the other of the following implementations was preferable to the other, and for what reasons? Would one be more efficient than the others. Would there be edge cases that are better one way or the other.
Solution 1:
extension Optional {
var isNil:Bool {
switch self {
case .None:
return true
case .Some:
return false
}
}
var notNil:Bool {
switch self {
case .None:
return false
case .Some:
return true
}
}
}
Solution 2:
extension Optional {
var isNil:Bool {
return self == nil
}
var notNil:Bool {
return self != nil
}
}