atomic-data-docs icon indicating copy to clipboard operation
atomic-data-docs copied to clipboard

Union / Sum Datatype

Open theduke opened this issue 3 years ago • 3 comments

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:

  • allow the datatype property to be an array of possible types
  • add an additional property for the Property class: somehting like alternativeDataTypes
  • require defining a custom datatype for each union. There could be a UnionType class which has a datatypes property, and custom union datatypes specify "isA: "Union". That's rather awkward though, since you could end up with StringOrInt` etc datatypes , which pollute the readability of a schema quite a bit, and more importantly might be duplicated a lot

Non of these is really ideal.

Curios about alternative thoughts here.

theduke avatar Jul 15 '22 13:07 theduke

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

theduke avatar Jul 15 '22 14:07 theduke

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.

theduke avatar Jul 15 '22 14:07 theduke

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?

joepio avatar Jul 16 '22 08:07 joepio