Utility Classes :: IsOneOf
Overview
The IsOneOf class is used to specify that the object must be an instance of any number of types or user-defined classes, or be validated by other extension classes [1].
The IsOneOf() constructor must be passed at least two ([2]) distinct ([3]) types, user-defined classes or registered extension classes. For example:
IsOneOf(int, FooClass, IsAllof(ClassA, ClassB))
Where FooClass is either a old- or new-style class, and IsAllOf is the IsAllOf() utility class.
The requirement that there are two distinct conditions stems from the fact that the following call is almost certainly an error:
IsOneOf(int, int)
Details
-
If the object being checked by the
IsOneOfinstance does not match any of the provided conditions, a_TC_TypeErrorwill be raised; therightattribute will be theIsOneOfinstance, and thewrongattribute will be the type of the object. [4]This exception is to be caught and converted to a
TypeCheckErrorbefore it reaches the level of the user. -
IsOneOfobjects will only compare equal (i.e.,==) if all the conditions in the left-hand side are found in the right-hand side, and vice versa [5].All
IsOneOfinstances that compare equal with==will produce the same value when run through the built-inhash()function [6]. -
The
IsOneOfclass has been given some intelligence about boolean equality, meaning that it will optimise the creation of certain instances. For instance:IsOneOf(IsOneOf(int, float), IsOneOf(str, set))
is equivalent to
IsOneOf(int, float, str, set)
In fact, the first will be coerced into the second, meaning that the two will compare and hash as equals [5, 6].
-
Another coercion-related intelligence is this:
IsOneOf(int, int, float, float)
will be simplified to
IsOneOf(int, float)
-
The order in which the
IsOneOfinstance's conditions will be checked is undefined.