Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliased tagged sums / map multiple tags #24

Open
samhh opened this issue Aug 11, 2023 · 1 comment
Open

Aliased tagged sums / map multiple tags #24

samhh opened this issue Aug 11, 2023 · 1 comment

Comments

@samhh
Copy link
Member

samhh commented Aug 11, 2023

It may be useful to be able to alias tags, nullary or not. This'd be somewhat equivalent to Serde's alias attribute. An example use case:

type Weather = Sum.Member<"Sun"> | Sum.Member<"Rain">
type Country
  = "UK" // Rain
  | "Italy" // Sun
  | "Spain" // Sun

It's possible with getCodecFromMappedNullaryTag however that's not terribly ergonomic, and it's undocumented what should happen when multiple members of one type map to a single member of the other.

@samhh samhh changed the title Aliased tagged sums Aliased tagged sums / map multiple tags Aug 11, 2023
@samhh
Copy link
Member Author

samhh commented Aug 11, 2023

I wonder if we could cheaply support this by extending getCodecFromPrimitiveMappedNullaryTag to support regex:

getCodecFromPrimitiveMappedNullaryTag(Weather)({
  Sun: /^(Italy|Spain)$/,
  Rain: "UK",
})

The overlap behaviour is unspecified. We should have it follow input order similar to the codecs implemented with foldToUnion (i.e. t.union).

How would this handle encoding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant