Woj*_*ilo 3 haskell types type-systems
是否可以case在Haskell中的两个不相关类型之间使用表达式,就像在此示例(不工作)代码中一样:
data A = A
data B = B
f x = case x of
A -> 1
B -> 2
main = do
print $ test A
return ()
Run Code Online (Sandbox Code Playgroud)
我知道我可以Either在这里使用,但是这段代码并不打算使用 - 我想深入学习Haskell类型系统并看看可以做些什么.
A并且B是不同的类型.如果您想要一个可以获取多种类型值的函数,则需要一个类型类.
data A = A
data B = B
class F a where
f :: a -> Int
instance F A where
f _ = 1
instance F B where
f _ = 2
main = do
print $ f A
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |