3 ms·
OK, yes, I can see how `fromJust`, more specifically, is the unsafe part. You're dereferencing a pointer which is probably non-nil but the compiler can't tell y
by wonderzombie 14y ago
OK, yes, I can see how `fromJust`, more specifically, is the unsafe part. You're dereferencing a pointer which is probably non-nil but the compiler can't tell you because you've insisted on checking it manually rather than communicating your intent to the compiler more directly.
So when you say "avoid fromJust," the alternative in practice is to use pattern matching, yes? The example you used above was a case statement. A guard clause is another obvious one, as are additional clauses (?) in a function.
- Peaker 14y agoThere are many alternatives to fromJust. They are all essentially implemented on top of the safe primitive: pattern-matching. Guard clauses in Haskell are actually boolean conditionals, and are boolean-blind. Safe pattern-matching can be done via the function definition sugar: f Nothing = ... f (Just x) = ... Which is equivalent to a "case". The various functions that work with a Maybe value make it non-tedious to do things. For example, instead of: case mx of Just x -> Just (x + 1) Nothing -> Nothing You can write: fmap (+1) mx Additionally, (>>=), fromMaybe and many other useful functions exist, and they're all based on safe pattern-matching.