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 sum types in the type system #1248

Closed
8 tasks done
alecthomas opened this issue Apr 13, 2024 · 3 comments
Closed
8 tasks done

Support sum types in the type system #1248

alecthomas opened this issue Apr 13, 2024 · 3 comments
Assignees
Labels
advanced Requires advanced understanding of FTL internals epic An umbrella topic

Comments

@alecthomas
Copy link
Collaborator

alecthomas commented Apr 13, 2024

Design

There are a few reasons to support sum types in FTL, but one of the primary motivations is mapping FTL types to JSON. JSON very frequently supports multiple types for a single key. eg. both of these might be valid keys:

{
  "depends": "foo"
}

and

{
  "depends": ["foo", "bar"]
}

We'll need to ensure that however we implement this, it's relatively simple to express sum types in each language, even languages like Go and Kotlin that don't provide first-class support for sum types.

eg. in Go this might look like:

//ftl:sumtype
type ScalarOrList interface { scalarOrList() }

type Scalar string
func (Scalar) scalarOrList() {}

type List []string
func (Scalar) scalarOrList() {}

type Response struct {
  Depends ScalarOrList `json:"depends"`
}
@alecthomas alecthomas added the advanced Requires advanced understanding of FTL internals label Apr 13, 2024
@github-actions github-actions bot added the triage Issue needs triaging label Apr 13, 2024
@github-actions github-actions bot removed the triage Issue needs triaging label Apr 15, 2024
@alecthomas
Copy link
Collaborator Author

alecthomas commented Apr 19, 2024

How are sum types represented in ingress JSON? {"kind": "Scalar", "value": "foo"}? In the example above this is probably redundant because we can tell from the type, but for data structures we wouldn't be able to. Though it might still be ambiguous in the case of eg. Bytes | String because Bytes is represented as a string, so probably better to be explicit.

@worstell
Copy link
Contributor

How are sum types represented in ingress JSON? {"kind": "Scalar", "value": "foo"}? In the example above this is probably redundant because we can tell from the type, but for data structures we wouldn't be able to. Though it might still be ambiguous in the case of eg. Bytes | String because Bytes is represented as a string, so probably better to be explicit.

yeah a lot to unpack here, updated the design with discussion around encoding/a proposed strategy

@worstell worstell added the epic An umbrella topic label May 3, 2024
@alecthomas
Copy link
Collaborator Author

Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
advanced Requires advanced understanding of FTL internals epic An umbrella topic
Projects
None yet
Development

No branches or pull requests

3 participants