3 ms·
Which languages? In the languages that I'm somewhat familiar that support Optionals (Kotlin and Swift) you can still get NPEs, there's just some nice syntax tha
by Rebelgecko 8y ago
Which languages? In the languages that I'm somewhat familiar that support Optionals (Kotlin and Swift) you can still get NPEs, there's just some nice syntax that makes it harder to do accidentally (e.g. by using !! or ! on a null/nil optional)
- UK-Al05 8y agoMost ML(Ocaml) languages I use will tell you if you haven't handled all the cases.
- jayshua 8y agoElm is an example of a language that literally can not produce NPEs. I use Rust on a daily basis and while it "technically" can produce one, I've never seen one.
- ajanuary 8y agoThe idiomatic way to use optional types is pattern matching (e.g. when in kotlin, switch in Swift). These pattern matching constructs tend to have a requirement that your cases are exhaustive. This makes it impossible to miss the None case. However, they also tend to provide escape hatches for getting at the type directly, which defeats that and will still let you NPE.
- timw4mail 8y agoRust does not allow null values without resorting to unsafe code.
- cesarb 8y agoTechnically, it does: std::ptr::null and std::ptr::null_mut are not unsafe. (Of course, you cannot try to dereference the resulting pointer without using unsafe.)