Type-value compatibility
Syntax
isCompatibleValue($value, $target)
Description
Checks whether a value value is compatible with a target type target. Here, target can be an object type (my.module:MyClass), an attribute type (my.module:MyClass#attribute) or a primitive type (e.g. `tl.core:String`). For attribute types, the defined conditions such as mandatory field (mandatory) and multiple values (multiple) are also taken into account. Value can be a single value or a collection. In the case of collections, the system checks whether all elements contained are compatible with the target type.
Parameters
| Parameter Name | Type | Type Description | Mandatory | Default |
|---|---|---|---|---|
| value | Any | The value to be checked, can be a single value or a collection. | yes | |
| target | type | The target type with which the value should be compatible. Can be an object type (e.g. my.module:MyClass), an attribute type (e.g. my.module:MyClass#attribute) or a primitive type (String, Double, Boolean, etc.). |
yes |
Return value
Type: Boolean value
true if the value can be used in the specified target type. This means specifically:
- For simple types (such as numbers, text): The value can be stored in this data type
- For object types: The object is compatible with the target type
- For attributes: The value fulfills all conditions of the attribute (e.g. mandatory field) and matches the attribute type
Examples
// Prüfung von Zahlen mit verschiedenen Typen
isCompatibleValue(123, `tl.core:Integer`) // true
isCompatibleValue(123.99, `tl.core:Double`) // true
// Prüfung von Text
isCompatibleValue("test", `tl.core:String`) // true
isCompatibleValue("123", `tl.core:Integer`) // false
// Prüfung von Wahrheitswerten
isCompatibleValue(true, `tl.core:Boolean`) // true
// Prüfung von Objekten
{
objektB = all(`test.isCompatibleValue:B`).firstElement();
isCompatibleValue($objektB, `test.isCompatibleValue:B`); // true - Objekt passt zum eigenen Typ
}
// Prüfung von Listen
isCompatibleValue([123, 456], `tl.core:Integer`) // true - alle Elemente sind kompatibel
// Prüfung mit Attributen (nicht-Pflichtfeld, mehrere Werte erlaubt)
{
objektB = all(`test.isCompatibleValue:B`).firstElement();
isCompatibleValue([$objektB], `test.isCompatibleValue:A#nonMandatoryMultipleClassB`); // true
}
// Prüfung mit Attributen (Pflichtfeld, nur ein Wert erlaubt)
{
objektB = all(`test.isCompatibleValue:B`).firstElement();
isCompatibleValue($objektB, `test.isCompatibleValue:A#mandatoryNonMultipleClass`); // true
isCompatibleValue(null, `test.isCompatibleValue:A#mandatoryNonMultipleClass`); // false - null ist bei Pflichtfeld nicht erlaubt
}