是否可能以新的价值扩大数据类型?
E.g.: The following compiles:
data Axes2D = X | Y
data Axes3D = Axes2D | Z
但如下:
data Axes2D = X | Y deriving (Show, Eq)
data Axes3D = Axes2D | Z deriving (Show, Eq)
type Point2D = (Int, Int)
type Point3D = (Int, Int, Int)
move_along_axis_2D :: Point2D -> Axes2D -> Int -> Point2D
move_along_axis_2D (x, y) axis move | axis == X = (x + move, y)
| otherwise = (x, y + move)
move_along_axis_3D :: Point3D -> Axes3D -> Int -> Point3D
move_along_axis_3D (x, y, z) axis move | axis == X = (x + move, y, z)
| axis == y = (x, y + move, z)
| otherwise = (x, y, z + move)
(move_along_axis_3D
) 评注没有给出错误:
Prelude> :l expandTypes_test.hs
[1 of 1] Compiling Main ( expandTypes_test.hs, interpreted )
expandTypes_test.hs:12:50:
Couldn t match expected type `Axes3D with actual type `Axes2D
In the second argument of `(==) , namely `X
In the expression: axis == X
In a stmt of a pattern guard for
an equation for `move_along_axis_3D :
axis == X
Failed, modules loaded: none.
So is it possible to make X
and Y
of type Axes2D
as well of type Axes3D
?
If it is possible: what am I doing wrong? Else: why is it not possible?