Should it be used to store error messages?

I have a Haskell function that accepts user input and another function that validates that input. Of course, the check may fail, in which case I would like to return an error message giving some feedback on what was done wrong.

I know that there are many ways I could do this. After a little experience, I have a better way to use Either String a. What throws me away is that I don't care a. Either it fails, and I would like to store more information, or it succeeds. ais lost.

Using Maybe Stringan acceptable way to store an error message? It feels the other way around, but completely ignoring the value to the right of it is Eitheralso very bad. What is canonical here?

+7
source share
5 answers

I recommend using Except String ()(or Either String ()) on top Maybe Stringfor several reasons:

  • , , , , . , , a String , , , , String -> Except String (Int, Int, Int) . , , Foo -> Except String (), - .
  • "" , , . , , , , , , . Monad Either ; :

    validatePerson p now = do
        age <- validateAge p
        date <- validateBirthdate p
        validateMatchingAgeAndDate age date now
    

    , , , . bigValidator v = option1 v <|> option2 v .

    Haskellers.

  • , Nothing . , , .

+10

. a, , , , . , Maybe String.

Either. , , , Either " ". , , Left error-ish Right success-ish, ab initio Either - , . , Haskell , Either a b, , a + b a || b.

Either String a "" a, - fmap (const ()) , Either String (), Maybe String, " String", , , .

, , Either, Maybe, Except String (). - , , , ExceptT String IO ().

+4

, Maybe String.

data Result = OK | Error String

newtype Result = Result (Maybe String)

Maybe, GeneralizedNewtypeDeriving,

newtype Result = Result (Maybe String)
   deriving (Eq, Show) -- etc.

(: , , , . , //, , .)

:

foo :: Result -> ...
foo (Result (Just x)) = ...
foo (Result (Nothing)) = ...

.

pattern OK      = Result Nothing
pattern Error x = Result (Just x)

, data, , newtype.

foo :: Result -> ...
foo (Error x) = ...
foo OK        = ...

, Result. , , , (?) Haskellers .

, , , data Result . , , , , , ( Prelude).

+4

, Maybe String ( ), , Maybe. Either String () ( / ), .

" ", , (.. ). SOOME- validation. :

>>> _Success # (+1) <*> _Success # 7 :: AccValidation String Int
AccSuccess 8

>>> _Failure # ["f1"] <*> _Success # 7 :: AccValidation [String] Int
AccFailure ["f1"]

>>> _Success # (+1) <*> _Failure # ["f2"] :: AccValidation [String] Int
AccFailure ["f2"]

>>> _Failure # ["f1"] <*> _Failure # ["f2"] :: AccValidation [String] Int
AccFailure ["f1","f2"]

, AccValidation m Monad. , / bind.

+3

.. , , , , , Haskell ( Haskell), . .


Maybe, Nothing, , Just msg . , , Maybe :

{-# LANGUAGE PatternSynonyms #-}

type Result = Maybe
pattern AllGood = Nothing
pattern Fail x = Just x

-- example usage
isValidBorrower :: [String] -> Result String
isValidBorrower args
  | length args /= 3 = Fail "Wrong number of arguments"
  | otherwise = AllGood

Maybe, MaybeFailure, , Maybe String , .

The disadvantage is that both of these functions still work:

foo :: Maybe String
foo = AllGood

bar :: Result String
bar = Nothing
+1
source

Source: https://habr.com/ru/post/1016338/


All Articles