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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With