从错误数据类型中获取数据

时间:2017-03-17 16:29:23

标签: haskell

如果我有错误数据类型:

data Error a = Fail|Ok a
           deriving (Eq, Ord, Show)

如果某些内容返回Error a,我怎么才能自己获取a,以便我可以使用它?

1 个答案:

答案 0 :(得分:4)

您需要使用pattern matching。例如:

case errorValue of
  Fail -> ... -- Do something if it was a fail
  Ok a -> ... -- Do something involving a if it was OK