tags:

views:

151

answers:

2

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 )

User.hs:3:0: Invalid type signature Failed, modules loaded: none.

Note: SO seems to have trouble parsing the image url I added, so here is the direct link (it's a screenshot of the error).

http://www.freeimagehosting.net/image.php?7a170fa86d.jpg

Type Error

+14  A: 

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.

Justice
+5  A: 

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
Logan Capaldo
Thanks for the additional info; that's a good tip.
Douglas Brunner