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

Support tsp union types #1091

Closed
jhendrixMSFT opened this issue Dec 4, 2023 · 6 comments
Closed

Support tsp union types #1091

jhendrixMSFT opened this issue Dec 4, 2023 · 6 comments
Labels

Comments

@jhendrixMSFT
Copy link
Member

TypeSpec defines union type which looks to be analogous to discriminated unions a la TypeScript. We might be able to emulate this via polymorphism (needs prototyping/experimentation).

@jhendrixMSFT
Copy link
Member Author

Three ideas.

  • polymorphism
  • closures
  • unexported type

@jhendrixMSFT
Copy link
Member Author

Given that union types are very minor and banned in Azure services, the current POR is to expose them as raw JSON, i.e. []byte.

@jhendrixMSFT
Copy link
Member Author

After discussion, we've decided to not add any support for union types.

CC @tadelesh

@tadelesh
Copy link
Member

do you mean leave it as a binary just like java and .net?

@jhendrixMSFT
Copy link
Member Author

jhendrixMSFT commented May 31, 2024

We won't add any support for it (it's a fair amount of work to make this happen as the tcgc representation needs modification to improve the codegen). So, if a tsp contains a union, the emitter will fail with an "unsupported kind union" error.

The reason we don't want to add binary support is because the usability is terrible. Sending isn't as bad, but when receiving, how is a customer supposed to understand what types are possible and how do they unmarshal it in a robust way?

Further, per conversations with @JeffreyRichter unions won't be allowed in tsp Azure services for this exact reason. In practice, this means that not supporting unions has little practical risk.

@tadelesh
Copy link
Member

tadelesh commented Jun 3, 2024

We won't add any support for it (it's a fair amount of work to make this happen as the tcgc representation needs modification to improve the codegen). So, if a tsp contains a union, the emitter will fail with an "unsupported kind union" error.

The reason we don't want to add binary support is because the usability is terrible. Sending isn't as bad, but when receiving, how is a customer supposed to understand what types are possible and how do they unmarshal it in a robust way?

Further, per conversations with @JeffreyRichter unions won't be allowed in tsp Azure services for this exact reason. In practice, this means that not supporting unions has little practical risk.

Thanks for explanation. I also dislike union and believe if do need union then service should be responsible for adding customization code for serialization and deserialization. Since Go only need to support MPG with TypeSpec for now, I'm good with the solution.

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

No branches or pull requests

2 participants