haskell - Adding a new constructor to a datatype after declaration -
i have requirement add new constructors datatype after it's module has been loaded. pseudo code may seem this:
import mymodule data mytype = newconstructor
in above example mytype
has been defined in mymodule
. there way that?
you can't. datatype closed. , thing otherwise, how can predict how previsouly defined function behave?
several workarounds exist, here off top of head:
- use function constructor. e.g.
myextendedtype = mytype 42
; - use own type , mapping function. e.g.
data myextendedtype = myextendedtype; tomytype myextendedtype = mytype 42
; wrap existing datatype:
import mymodule old data myextendedtype = fromold (mytype int) | myextendedtype foo (fromold m) = old.foo m foo myextendedtype = undefined
Comments
Post a Comment