Question
Extending types in a backward compatible way?
Let's say I have:
data X = X Int Char Whatever
Now let's say I want to add a type parameter to X
like so:
data Version = OldVersion | NewVersion
data X (phantomParam :: Version) = X Int Char Whatever
I'm doing this because internally, I'd like to vary this parameter to change the behavior of some instances.
But I'd like any code I don't touch to still work.
So my first thought was to do the following:
data X' (phantomParam :: Version) = X Int Char Whatever -- note the dash
type X = X' OldVersion
Then all existing code should be unchanged. Which almost works, except now when I export:
X(X)
I get an error.
That's not a big deal, because I can export:
X, X'(X)
But now I have to also change all imports from:
X(X)
to
X, X'(X)
presumably breaking all code using this. Is there any way to do this refactoring without breaking downstream code? I'm thinking pattern synonyms might be helpful here but I'm not exactly able to get this to work.