3 ms·
There are unsafe escape hatches for the rare cases where you do need them, but they aren't recommended. It's not the idiomatic usage, which is why the original
by the_af 8y ago
There are unsafe escape hatches for the rare cases where you do need them, but they aren't recommended. It's not the idiomatic usage, which is why the original post in this thread saying "it'll blow up when I do Optional.get" is mistaken.
If it's provably non-nil, use get. But you must provide the proof, and your first reaction must always be "I'm not sure I can assume this".
> What would you write in the nil case of the pattern matching anyway? return -1? That's absurd.
Yes, it's absurd. You don't return this type of sentinel values in statically typed languages with optional types. If you don't need to discriminate the error, sometimes returning Nothing will suffice. If you need the error, you can use an Either type (where traditionally Left is used for the error).
Again, this isn't something I made up. It's how Optionals are used. I suggest you read the link I posted above.