This Flexscript code throws an exception, and the following appears in System Console:
exception: FlexScript exception: label notexists does not exist at /0 c: /testlink_instance i: /testlink_associated
There is no exception handling mechanism in Flexscript, and no way to see where the exception was thrown, which is a problem in itself for more complex models.
Even if there were an exception handling mechanism in Flexscript, dealing with missing values is such a common use case, that it deserves to be addressed.
Many statically typed languages moved towards using an optional type (Maybe in Haskell, std::optional in C++, java.util.Optional in Java). This type may contain just a single value or nothing, and usually offers means to check if it's empty or to obtain a value of the contained type in a safe way. For instance, in C++ it is possible to
- - use optional<T> in conditional expressions (non empty values are evaluated as true)
- - use optional<T>::value_or(default_value) to avoid exceptions
class Variant in Flexsim is supposed to support empty values already (if Variant::type equals VariantType::Null), but other objects don't seem to use this feature, and the public methods of the class don't allow to work productively with null variants.
The proposal is two-fold:
- return null Variants rather than throwing an exception in LabelsArray::operator[], SubnodesArray::operator[]; or introduce a new safe element accessor (Variant& LabelsArray::get()) to avoid changing the signature of operator[].
- extend the Variant class with methods which allow to check if it is empty or replace a missing value with a default value safely:
bool Variant::isnull() const; Variant Variant::or(const Variant& defaultValue) const;
Variant::operator bool() should already work properly.
This is how how it may look for Flexscript users:
Implementation of this proposal would have minimal impact on the Flexscript language itself (no need to introduce new control flow structures). The Variant class needs only a couple of new public methods to support this feature. It would eliminate a huge source of exceptions which are not user-friendly (it's difficult to find the code which triggers them, it's impossible to handle exceptions in Flexscript). The new missing value idiom is consistent with the solutions developed for other languages, and encourages users to be explicit about how to handle missing values.
Old rank() and label() were already safe. New [] operator on .subnodes and .labels throws an exception, which interrupts the script and cannot be recovered from.