You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been using functions like this for a while:
unsafeFromJust::foralla. String->Maybea->a
unsafeFromJust msg = case _ ofNothing-> unsafeCrashWith $ "unsafeFromJust: " <> msg
Just a -> a
unsafeFromRight::forallea. Showe=>String->Eitherea->a
unsafeFromRight msg = case _ ofLeft e -> unsafeCrashWith $ "unsafeFromRight: " <> msg <> "; Left " <> show e
Right a -> a
instead of Partial once, like:
fromJust::foralla. Partial=>Maybea->a
fromJust (Just x) = x
Reason is when you wrote partial code you had some assumptions which are true, but then things change and when assumptions are violated, instead of looking at Failed pattern match at .... I prefer to see better error message like:
unsafeFromRight: `0xAA` must be valid HexString; Left "unexpected 0"
this is how I'm using this:
someConstant::HexString
someConstant = unsafeFromRight "`0xAA` must be valid HexString"
$ HexString.parse "0xAA"`
Would like to propose to add this functions to ps-maybe and ps-either, but opened this issue here for kick starting discussion. Any thoughts?
The idea of the Partial class was to get rid of “unsafe” prefixes for functions that can cause runtime errors, and to have the ability to clearly indicate the possibility of runtime errors in the type system, so I don’t want to go back on that and start adding new functions which can cause runtime errors. At least I think this function would need to have a Partial constraint.
I can imagine being on board with
fromJustBecause :: forall a. Partial => String -> Maybe a -> a
fromJustBecause msg = fromMaybe (crashWith ("fromJustBecause: " <> msg))
Then again, I’m not sure if my fromJustBecause meets the Fairbairn threshold (where the effort of remembering the name of the thing is outweighed by the effort of inlining it). Not including the function in this library has the advantage that the user gets to choose whether to use crashWith or unsafeCrashWith too, I suppose.
I think I'm against this because of that threshold. Besides, such a function could be added to some utils library if one wanted it but did not want to redefine it every time it was used.
Activity
hdgarrood commentedon Oct 13, 2020
The idea of the Partial class was to get rid of “unsafe” prefixes for functions that can cause runtime errors, and to have the ability to clearly indicate the possibility of runtime errors in the type system, so I don’t want to go back on that and start adding new functions which can cause runtime errors. At least I think this function would need to have a Partial constraint.
I can imagine being on board with
hdgarrood commentedon Oct 14, 2020
Then again, I’m not sure if my
fromJustBecausemeets the Fairbairn threshold (where the effort of remembering the name of the thing is outweighed by the effort of inlining it). Not including the function in this library has the advantage that the user gets to choose whether to usecrashWithorunsafeCrashWithtoo, I suppose.JordanMartinez commentedon Oct 14, 2020
I think I'm against this because of that threshold. Besides, such a function could be added to some utils library if one wanted it but did not want to redefine it every time it was used.