3 ms·
That is what Objective C provides (the author here calls it “viral null”). It works, but I think a better approach is the Swift way. In this a nullable type is
by jakear 8y ago
That is what Objective C provides (the author here calls it “viral null”). It works, but I think a better approach is the Swift way. In this a nullable type is precisely an Optional, as you stated, and they don’t respond to anything besides “isNull?” and “get” (the latter crashing the program if the former returns true). This way when you need to say “I might not know”, your type signature will express that, and the type system will guarantee everyone abides by it.
- hboon 8y agoThat Objective-C behaviour is called Message eating nil (or null in other languages).
- greiskul 8y agoAgreed, the swift way is the correct way. For cases where you do want the Objective C behavior, you can just use the ?. operator. This way you get compile times guarantees by the type system, and where you actually want to use the "viral null" you can, and it's very explicit for people reading the code what it's doing.