Reputation: 312
I am trying to define a new datatype for directions. But, I am getting an error "Not a data constructor: left" when I load the file in ghci.
Here is the code snippet:
data Dir = left
| right
| up
| down
instance Show Dir where
show left = "left"
show right = "right"
show up = "up"
show down = "down"
-- initialpos is assumed to be valid one
move::Pos->Dir->Pos
move prevPos left = ( (-1) $ fst prevPos , snd prevPos)
move prevPos right = ( (+1) $ fst prevPos , snd prevPos )
move prevPos up = ( fst prevPos, (+1) $ snd prevPos )
move prevPos down = ( fst prevPos, (-1) $ snd prevPos )
Please help me in figuring out what is wrong.
Upvotes: 0
Views: 2928
Reputation: 21757
As @chepner rightly points out, the data constructors should begin with capital letters. To address the next problem you run into i.e. Left
and Right
are ambiguous between your defined ones and those from Either
, you can do this:
Explicitly import Prelude and hide Either
type constructor to avoid the ambiguity. This will let you continue using all the other 'default' functions from Prelude
without issue
import Prelude hiding (Either(..)) --Everything except Either which causes ambiguity issues
(Optional) Do a qualified import of Data.Either
if you wish to use it elsewhere in your code.
import qualified Data.Either as E --You will be able to use Either by writing E.Either in your code
That said, you could avoid this trouble by renaming your data constructors if that is an option.
Upvotes: 2
Reputation: 532528
Ignoring infix data constructors, a data constructor must begin with a capital letter.
data Dir = Left | Right | Up | Down
Upvotes: 6