Gabriella439 / Haskell-Errors-Library

Type-safe error handling
BSD 3-Clause "New" or "Revised" License
64 stars 22 forks source link

The "Elvis" utility operator #22

Closed nikita-volkov closed 10 years ago

nikita-volkov commented 10 years ago

Some time ago I brought a suggestion on the "libraries" mailing list up to include this function in the Prelude, but got rejected. I think it would fit nicely in this library accompanying the already existing ?? and !?.

(?:) :: Maybe a -> a -> a
maybeA ?: b = fromMaybe b maybeA
{-# INLINE (?:) #-}

For motivation here is a quote from my initial message on the mailing list:

*Use cases:*

    1.
        maybeValue ?: error "Value is unexpectedly empty. This is a bug."

    instead of

        fromMaybe (error "Value is unexpectedly empty. This is a bug.")
maybeValue

    2.
        maybeA ?: maybeB ?: c

    instead of

        fromMaybe (fromMaybe c maybeB) maybeA

*Name collisions:*

    Hayoo search gives only 6 collisions with 5 hardly fundamental
libraries.

* The nickname and the operator itself are inspired by the ones from Groovy
language.

** The symbols of the operator are a reminder of plain old ternary
construct.

*** To understand the nickname look at the operator as on emoticon.   
Gabriella439 commented 10 years ago

I agree that this operator is appropriate for the errors library. My only request is to change the annotation to INLINABLE instead of INLINE, but other than that it looks good. If you write up a pull request I will merge it in. I can document it if you want.