3

With https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#extension-DuplicateRecordFields, you can do something like this:

data X = X { a :: A }
data Y = Y { a :: A }

f X{a = a} = a
g Y{a = a} = a

But I don't see something like this for constructors, like:

data X = A
data Y = A

f :: X -> ()
f A = ()

g :: Y -> ()
g A = ()

Couldn't a similar thing to the record syntax extension be done here? Where if it's ambiguous, it would be a compiler error, but if the type was annotated, it wouldn't be ambiguous and it should be legal?

I tried looking for existing proposals, but I couldn't find anything like this.

Rahul Manne
  • 1,229
  • 10
  • 20
  • Cross-site duplicate of [Why are data constructors with the same name disallowed in different type constructors?](https://softwareengineering.stackexchange.com/q/245810/275017) – Joseph Sible-Reinstate Monica Feb 13 '20 at 02:48
  • 1
    That is the same question, thanks for calling it out. But I'm not particularly satisfied with the accepted answer: "There is an ongoing effort to bring Overloaded or Duplicate record fields into Haskell.", since this doesn't answer if there is any ongoing effort to bring Overloaded or Duplicate constructor names. – Rahul Manne Feb 13 '20 at 03:03
  • I voted to close. It's quite impossible to answer "is there a language extension proposal that does X?" in a future-proof way (answer "no" and somebody may write one, answer "yes" and it may get implemented, both actions invalidating the otherwise correct answer!), which makes it a bad fit for the StackOverflow format. – Daniel Wagner Feb 13 '20 at 05:29
  • @DanielWagner Fair point, I was just really hoping I missed something and there was already an extension or proposal but just named something else in a way I managed to miss it while searching. – Rahul Manne Feb 13 '20 at 06:55
  • The desired functionality (having a constructor which works with more than one type) can be implemented using pattern synonyms, I think that would be a useful answer – oisdk Feb 13 '20 at 16:19
  • @oisdk Doesn't that only work in cases when all of the data constructors have the same number of fields? (Also, you can post it as an answer on the dupe target, which is still open.) – Joseph Sible-Reinstate Monica Feb 13 '20 at 16:47
  • Yes I think so (although there might be a way around that, with typeclass pattern synonym instances). – oisdk Feb 15 '20 at 02:17

0 Answers0