type-error

Available in: KerML, SysML

This checks that assigned values conform to the types of the element they are being assigned to.

Note: we are aware that type checking can be performed on other elements as well, for example validating that flow ends conform to payload type. This will be added in a future release.

To reduce the rate of false-positives, certain standard library functions and operators propagate types based on their inputs, i.e. are treated as generic functions:

attribute def A;
attribute x [*]; // can be anything

// `select` expression is inferred to return a sequence of `A`
attribute y : A = x.?{ in v; v istype A }; // ok

Similarly:

attribute def A;
attribute x [*] : A;

// `head` inferred to return an `A` from sequence `x`
attribute y : A = SequenceFunctions::head(x); // ok

Additionally, generic types are not assignable to derived types:

attribute def Generic;
attribute def Derived :> Generic;
attribute x : Derived = new Generic(); // error

While Derived is Generic through specialization, Generic is not strictly Derived as Generic may also be any one of other derived types that do not superset Derived. That is, the reverse attribute x : Generic = new Derived(); would be accepted, but not the other way around.

Example

attribute x : ScalarValues::String = true; // error

Boolean is not a string:

attribute x0 : ScalarValues::Boolean = true;    // ok
attribute x1 : ScalarValues::String = "string"; // ok

Assigning literal values to custom scalar types is an error:

attribute def MyReal :> ScalarValues::Real;
attribute x : MyReal = 4.0; // error

Either use a constructor expression to create the custom scalar:

attribute def MyReal :> ScalarValues::Real;
attribute x : MyReal = new MyReal(4.0); // ok

Or an alias:

alias MyReal for ScalarValues::Real;
attribute x : MyReal = 4.0; // ok

While full support for quantities is not yet implemented, assigning a quantity to a unit is an error:

attribute a : ISQ::LengthUnit = 4 [SI::m]; // error