atomicdata-dev / atomic-data-docs

Atomic Data is a specification to make it easier to exchange data.
https://docs.atomicdata.dev
MIT License
17 stars 7 forks source link

Union / Sum Datatype #126

Open theduke opened 1 year ago

theduke commented 1 year ago

Values can often be one of several datatypes (like string or int, class A or class B, ...).

There should be a way to model unions (anonymous or tagged).

Describing this in the schema is a bit tricky though, since the datatype property is just a single value.

Options:

Non of these is really ideal.

Curios about alternative thoughts here.

theduke commented 1 year ago

Ah, I see there is some overlap here with the allows-only concept in #27 .

theduke commented 1 year ago

Ah, an additional idea - which is actually kinda-sorta what I implemented in factordb:

Allow datatype to be a nested resource , although constrained to class types that refine existing datatypes with constraints.

joepio commented 1 year ago

The Union datatype can be useful, but it introduces a cost for data consumers: they will now need to perform type checking.

In programming languages like Rust, a Union type therefore does not exist by design. However, they do have enum values, which can definitely be used as Unions.

In atomic data, we do have enums, but these function more like JS enums (a list of options that do not contain values) instead of rust enums (a list of options that may contain any type of other value).

So I agree that currently, Atomic Data lacks a way to properly express something like Rust Enums or TS Unions.

Can you perhaps come up with a concrete real world example of something that you can't model right now with Atomic Data?