Reputation: 1043
I am currently experimenting with data types and I've ran into an issue involving multiple declarations of data constructors.
data DBPosition = Unknown
| Omega Integer
| Delta Integer
deriving (Show, Eq, Ord)
data DBGeometry = Unknown | Cis | Trans
deriving (Show, Eq, Ord)
data DoubleBond = DoubleBond DBPosition DBGeometry
deriving (Show, Eq, Ord)
If I was to make a value such as - let bond = DoubleBond Unknown Unknown
, then it could be inferred that the first Unknown
has a type of DBPosition
while the second Unknown
has a type of DBPosition
. Unfortunately this is not the case:
test.hs:6:27:
Multiple declarations of `Unknown'
Declared at: test.hs:1:27
test.hs:6:27
Failed, modules loaded: none.
Are there any language extensions that can be used to get around this?
Upvotes: 5
Views: 1324
Reputation: 34378
As Carsten pointed out above, your definition does not work because you have two constructors with the same name. You'd need to use e.g. UnknownDBPosition
and UnknownDBGeometry
. However, I'd argue a better solution arises from recognising:
Unknown
is not actually a variety of double bond geometry or position.That being so, I recommend that you remove both Unknown
and use Maybe
to specify lack of knowledge.
data DBPosition = Omega Integer
| Delta Integer
deriving (Show, Eq, Ord)
data DBGeometry = Cis | Trans
deriving (Show, Eq, Ord)
data DoubleBond = DoubleBond (Maybe DBPosition) (Maybe DBGeometry)
deriving (Show, Eq, Ord)
Upvotes: 10