Closed nikita-volkov closed 10 years ago
unsafe generally implies things that can break the type system or cause a segfault, not something partial, so I wouldn't want to go in that direction. Maybe it's time to introduce the Failure typeclass and pull in safe-failure.
@jwiegley might be interested in that proposal.
@snoyberg What about Vector.unsafeHead?
That's a perfect example. Vector.head is memory safe and will throw an exception if the vector is empty. unsafeHead has the possibility of a segfault, but in exchange is a little bit faster.
@snoyberg Am certainly interested. What can I do to help here? I'm really the way that Failure manages error handling in my recent code.
I don't think any help is necessary, I thought I'd just let you know that there's a possibility of including more direct failure assistance in classy-prelude.
I am not against including the versions from the safe package, but personally I will start experimenting with NonNull from mono-traversable
I've found that one thing unsafe
typically means is: it seems to work just fine at first, but comes back to bite you later. Nearly all of my segfaults in FFI code could be tracked down to an unsafeSomethingOrOther which worked just fine for a few months before memory shifted around and suddenly it wasn't safe to do anymore.
This should be addressed by the export of head
and family from Data.MinLen
in the next release.
I get that we don't export those functions because they are partial. I have two suggestions on that matter:
headOption
method, we can export their "maybe"-suffixed versions (e.g.,headMaybe
), which will returnNothing
in case of empty collection.unsafePerformIO
) and exporting the safe versions in their place. I.e., exporthead :: [a] -> Maybe a
andunsafeHead :: [a] -> a
, but, of course, generalized with typeclasses.First option doesn't break any existing conventional definitions. Second one follows the same "we know better" questionable approach as with the
fold
function.What do you think?