
On Fri, 25 Nov 2016 10:19:14 +0100
Daniel Trstenjak
Hi Rahul,
On Fri, Nov 25, 2016 at 12:06:06PM +0530, Rahul Muttineni wrote:
data X = A1 { name :: String, d :: Double} | A2 { name :: String, i :: Int} | A3 { name :: String, d1 :: Double, i1 :: Int}
Now you can use `name` directly to get the string component of the different variants.
It's not recommended to mix record syntax and ADTs, because you can get runtime errors that the compiler can't catch during compile time, like calling:
i (A1 "foo" 3.2)
agreed. I'm doing this as a form of shorthand instead of creating a text file based input and parsing it. Having any sort of type checking is an advantage.
If you're having the same field in all variants, then an other approach might be better:
data A = Ai Int | Ad Double | Aid Int Double
data X = X { name :: String, a :: A }
yes, that would be better. interestingly in my journeys through the intertubes I have not found a single mention of using the "()" syntax in place of the "{fieldname=value, ...}" syntax as the generator. Brian