Haskell type error using supposed type

I created a data type for storing basic user data and loaded it into ghci. Then I used ghci to search for a new signature like data type. I copied the type signature from ghci to the source file and tried to reload the file. Ghci made a mistake.

Below is the code and error.

My question is: why does this throw an error. I used the type that ghci inferred.

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] Compilation User (User.hs, interpreted)

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

+4
source share
2 answers

You can declare a value type (for example, a function), but you cannot declare a data type or data constructor using the syntax for declaring a type for values. In fact, you already declare the full type of the data type and data constructor when defining it, so there is no need for an additional type declaration. So just leave the line User :: ... ; this line is a syntax error because it is a User with capital U (constructor), and only lowercase names (values) can have types.

+14
source

By the way, if you want to write User in the "annotationy" y style, you can do this using the GADT syntax:

 {-# LANGUAGE GADTs #-} data User where User :: Int -> String -> String -> String -> String -> User 
+5
source

All Articles