Re:Java 8’s new Optional type doesn't solve anything
by steveklabnik on Monday October 26, 2015 @09:19AM
(#10451175)
unwrap() is not unsafe in Rust, and I don't think Haskell has anything similar?
[ Parent
| on HN ]
Re:Java 8’s new Optional type doesn't solve anything
by danieldk on Monday October 26, 2015 @09:53AM
(#10451397)
Sorry for the confusion! I meant 'unsafe' as in partial (not safe for all inputs). Not as in Rust's unsafe keyword.
Re:Java 8’s new Optional type doesn't solve anything
by steveklabnik on Monday October 26, 2015 @09:58AM
(#10451422)
Ah, right. It's really important in the context of Rust. :) People sometimes claim that unwrap() violates memory safety, which isn't true.
Re:Java 8’s new Optional type doesn't solve anything
by codygman on Monday October 26, 2015 @08:49PM
(#10455496)
The definition in Rust's documentation[0] was this:
let x: Option<&str> = None;
assert_eq!(x.unwrap(), "air"); // fails
The equivalent Haskell if unwrap throws a panic as the docs[0] seem to imply: λ> fromMaybe (error "panic!") Nothing
*** Exception: panic!
You could also do something like this if you don't mind dealing with the wrapped boolean value: λ> fmap (== "air") (Just "air")
Just True
and the failure case: λ> fmap (== "air") (Nothing)
Nothing
0: https://doc.rust-lang.org/std/option/enum.Option.html#method...
Re:Java 8’s new Optional type doesn't solve anything
by steveklabnik on Tuesday October 27, 2015 @08:43AM
(#10457591)
Right, but it's still not unsafe. The stack unwinds, destructors get called.
And I meant some notion of 'safety', not fromMaybe/fromJust.