I would like to define a tuple of two numbers as below:
scalar type container extending tuple<int16, int16> {
constraint expression on (
__subject__.0 >= 0
and __subject__.1 >= 0
and __subject__.0 < 256
and __subject__.1 < 256
and __subject__.0 <= __subject__.1
)
}
Is this possible?
I would prefer this over defining a full object type, because I am hoping a tuple type will also serialize to a tuple type in my programming language (TypeScript and Rust), while an object would serialize to an interface/struct.
As of version 4.2 it's not possible to extend the
tuple
. Most you can do is to use a constrainedint
inside of the tuple:Inserting a value gives a custom error:
Alternatively, you can use
range
which funny enough by default watches the lower bound to be less than the upper, but is not able to constraint the min/max, so we must do it explicitly:Here the
start <= end
invariant has default error message.And we can see that the custom one works too:
Note that since range is not overloaded with
int16
, but it is withint32
, we might try to define ouruint8
as:But we would discover, that range won't accept that:
Here running
edgedb migration create
yields:Sadly, the
range
does not satisfy your need to serialize to tuple. Withrange
in TypeScript you get theStill this might be an interesting option, given the possibility of an empty range.