我在使用幻象類型的困惑:幻影類型混淆?
type Words = String
type Numbers = Int
data NonPhantom = NP1 Words | NP2 Numbers deriving (Show)
data Phantom a = P1 Words | P2 Numbers deriving (Show)
nonPhantomFunction :: NonPhantom -> Int
nonPhantomFunction r = 100
phantomFunction :: Phantom Numbers -> Int
phantomFunction a = 2001
main = do
print $ nonPhantomFunction (NP1 "sdsdds") --can also pass NP2 here!
print $ phantomFunction (P1 "sdsdsd") --This shouldn't work!?
我希望這個代碼不編譯,因爲phantomFunction
明確規定其預期的數據類型的Numbers
Phantom
。
但是這個編譯好嗎?我究竟做錯了什麼?
也許你想要一個GADT呢? – augustss