What is the best way to do C-struct in haskell? -
this question has answer here:
- avoiding namespace pollution in haskell 4 answers
currently use data fields
mimic c-struct. found unlike domain-driven way of programming object.property
, in haskell property names dumped module namespace. creates problems when have more 1 such struct. example, if have 2 data types:
data person = person { name :: text, address :: text } data dog = dog { name :: text, breed :: text }
then ghc complain: multiple declarations of ‘name’
. have name "properties" prefixes:
data person = person { getpersonname :: text, getpersonaddress :: text } data dog = dog { getdogname :: text, getdogbreed :: text }
is necessary? or using wrong way define struct?
you can use -xdisambiguaterecordfields
extension. allows this: using same name of record label in multiple data declaration.
however, should think whether needed. if 2 data types contain same conceptual thing, perhaps better remove field both types , use in common wrapper instead?
data person' = person { address :: text } data dog' = dog { breed :: text } data named b = named { name :: text, being :: b } type person = named person' type dog = named dog'
Comments
Post a Comment