What if a value constructor should also be a type?
I am in case that a value constructor of a data type should be also a type, e.g if I have:
data Person = Student {开发者_如何学JAVAname :: String, age :: Int}
| Staff {name :: String, salary :: Float} deriving (Show)
I misused like this way:
type St = Student
or
anywhere that use Student as type, e.g [Student]
is wrong too
I know I can't do this, because Student
is not a type! Then how to make the value constructor Student
can be used as a type? Is it out of law?
So you want Student
to be a type distinct from Person
and Staff
? Make them different types, then.
data Person = StudentPerson Student
| StaffPerson Staff
deriving (Show)
data Student = Student {name :: String, age :: Int} deriving (Show)
data Staff = Staff {name :: String, salary :: Float} deriving (Show)
They're different types now. You can control what it is you want. And you don't accidentally generate partial functions like age
and salary
are in your case, either.
精彩评论