Haskell type error using inferred type
I created a data type to hold basic user information and loaded it into ghci. I then used ghci to look at the new data types type signature. I copied the type signature from ghci into the source file and tried to reload the file. Ghci threw an error.
The code and error are below.
My question is, why is this throwing an error. I used the type that was inferred by ghci.
User :: Int -> String -> String -> String -> String -> User
data User = User { userID :: Int,
login :: String,
password :: String,
username :: String,
email :: String
} deriving (Show)
Prelude> :r User [1 of 1] Compiling User ( User.hs, interpreted 开发者_StackOverflow社区)
User.hs:3:0: Invalid type signature Failed, modules loaded: none.
You may declare the type of a value (such as a function), but you may not declare the type of a data type or of a data constructor using the type declaration syntax for values. In fact, you already are declaring the full type of the data type and data constructor when you define them, so there is no need for an additional type declaration. So just leave out the line User :: ...
; that line is a syntax error because it User
with a capital U (constructor) and only lower-case names (values) may have ascribed types.
Incidentally, if you would like to write User in a "type annotationy"y style, you can do so with the GADT syntax:
{-# LANGUAGE GADTs #-}
data User where
User :: Int -> String -> String -> String -> String -> User
精彩评论