应该用Maybe来保存错误信息? [英] Should Maybe's be used to hold error messages?
问题描述
我有一个Haskell函数,它接受用户输入,另一个函数验证这个输入。当然,验证可能会失败,在这种情况下,我想返回一条错误消息,提供有关错误操作的一些反馈。
我知道有很多方法我可以做到这一点。在我有一点经验后,似乎最好的方法是使用任一字符串a
。抛开我的是我不在乎 a
。要么它失败了,我想存储更多信息,否则它会成功。 a
被浪费了。
正在使用 Maybe String
一种可接受的方式来存储错误消息?它对我来说倒退了,但完全忽略了右边的值
也感觉很糟糕。这里是什么规范?
我鼓励使用 Except String()$ c $由于以下原因,c>(或
或者String()
)在可能的字符串
之上
- 稍后您可能会发现,您的验证函数返回数据结构的某些部分很方便。例如,在验证
String
是电话号码的同时,您可能想要返回区号,号码的第一部分和第二部分,并给出验证类型,如字符串 - >除String(Int,Int,Int)
或类似的。使验证器不返回任何有趣的内容,请键入Foo - >除了String()
,它们只是这种模式的一个特例 - 因此更容易组合在一起。 继续使用fit together部分,你稍后可能会发现你想用较小的验证器来构建一个大的验证器。也许你有一个验证器来检查一个人是否已经指定了有效的年龄和出生日期,并且想从这个验证器中构建一个验证器,该验证器还检查该年龄是否适合出生日期。的
Monad
实例在这里会有所帮助;例如:
validatePerson p now = do
年龄< - validateAge p
日期< - validateBirthdate p
validateMatchingAgeAndDate现在的日期
或者有两种方法可以验证某个值正确,你想允许。然后
bigValidator v = option1 v< |> option2 v
是一种廉价和令人愉快的方式来结合验证的两种方式。
作为一个副作用,这些方法将现有的验证器使其他Haskeller可以立即识别更大的。
-
有一个非常强大的约定:
Nothing
是失败的。使用相反的约定不是一个问题 ,但它可能会让其他贡献者感到困惑,并且可能会在将来对您自己产生潜在的影响。 - You will probably find later that your it is convenient for your validation function to return some parts of the data structure. For example, while validating that a
String
is a phone number, you might want to return the area code, first, and second parts of the number, giving a validation type likeString -> Except String (Int, Int, Int)
or similar. Making validators that don't return anything interesting have typeFoo -> Except String ()
makes them just a special case of this pattern -- and therefore easier to fit together. Continuing the "fit together" part, you may later find that you want to construct one big validator out of smaller ones. Perhaps you have a validator that checks that a person has specified a valid age and birth date, and want to build a validator out of this that also checks that the age is about right for the birth date. The
Monad
instance forEither
will help here; for example:validatePerson p now = do age <- validateAge p date <- validateBirthdate p validateMatchingAgeAndDate age date now
Or perhaps there are two ways for some value to validate properly and you want to allow either. Then
bigValidator v = option1 v <|> option2 v
is a cheap and cheerful way to combine the two ways of validating.As a side benefit, these methods of combining existing validators to make bigger ones will be instantly recognizable to other Haskellers.
There is a very strong convention that
Nothing
is a failure. Using the opposite convention is not a problem, necessarily, but could be confusing to other contributors and potentially to yourself far in the future.
I have a Haskell function which takes user input and another function which validates this input. Of course, the validation could fail, in which case I would like to return an error message giving some feedback on what was done incorrectly.
I know that there are many ways that I could do this. After the little experience I have, it seems like the best way is to use Either String a
. What is throwing me off is that I don't care about the a
. Either it fails and I would like to store more information, or it succeeds. The a
is wasted.
Is using Maybe String
an acceptable way to store an error message? It feels backwards to me, but completely ignoring the value in the right of an Either
feels pretty bad too. What is canonical here?
I encourage the use of Except String ()
(or Either String ()
) over Maybe String
, for a few reasons:
这篇关于应该用Maybe来保存错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!