Is it possible to declare a constrained data type using record syntax? My attempt
data ConstrainedRecord a where
ConstrainedRecord :: Num a => { first :: a, second :: a }
causes GHC to complain "Record syntax is illegal here".
Yes, but in GADT syntax you always have to explicitly specify the return type of the constructor:
data ConstrainedRecord a where
ConstrainedRecord :: Num a => { first :: a, second :: a } -> ConstrainedRecord a
(Also, this may come handy:
deriving instance (Show a) => Show (ConstrainedRecord a)
using StandaloneDeriving
, since a normal deriving
does not work.)
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