11.1. Miscellaneous contract annotations
The CONTRACTS module provides compile-time type constraints for generic function
arguments. Annotations like [expect_any_array], [expect_any_enum],
[expect_any_numeric], and [expect_any_struct] restrict which types
can instantiate a generic parameter, producing clear error messages on mismatch.
See Contracts for a hands-on tutorial.
All functions and symbols are in “contracts” module, use require to get access to it.
require daslib/contracts
Example:
require daslib/contracts
[!expect_dim(a)]
def process(a) {
return "scalar"
}
[expect_dim(a)]
def process(a) {
return "array"
}
[export]
def main() {
var arr : int[3]
print("{process(42)}\n")
print("{process(arr)}\n")
}
// output:
// scalar
// array
11.1.1. Function annotations
- contracts::expect_any_array
[expect_any_array(argname)] contract, which only accepts array<T>, T[], or das`vector<T>
- contracts::expect_any_enum
[expect_any_enum(argname)] contract, which only accepts enumerations
- contracts::expect_any_bitfield
[expect_any_bitfield(argname)] contract, which only accepts bitfields
- contracts::expect_any_vector_type
[expect_any_vector_type(argname)] contract, which only accepts vector types, i.e. int2, float3, range, etc
- contracts::expect_any_struct
[expect_any_struct(argname)] contract, which only accepts structs (but not classes)
- contracts::expect_any_numeric
[expect_any_numeric(argname)] contract, which only accepts numeric types (int, float, etc)
- contracts::expect_any_workhorse
[expect_any_workhorse(argname)] contract, which only accepts workhorse types (int, float, etc) Workhorse types are: bool,int*,uint*,float*,double,range and urange, range64 and urange64, string,enumeration,and non-smart pointers
- contracts::expect_any_workhorse_raw
[expect_any_workhorse_raw(argname)] contract, which only accepts workhorse types which are raw (not pointer or bool)
- contracts::expect_any_tuple
[expect_any_tuple(argname)] contract, which only accepts tuples
- contracts::expect_any_variant
[expect_any_variant(argname)] contract, which only accepts variants
- contracts::expect_any_function
[expect_any_function(argname)] contract, which only accepts functions
- contracts::expect_any_lambda
[expect_any_lambda(argname)] contract, which only accepts lambdas
- contracts::expect_ref
[expect_ref(argname)] contract, which only accepts references
- contracts::expect_pointer
[expect_pointer(argname)] contract, which only accepts pointers
- contracts::expect_class
[expect_class(argname)] contract, which only accepts class instances
- contracts::expect_value_handle
[expect_value_handle(argname)] contract, which only accepts value handles
11.1.2. Type queries
- contracts::isYetAnotherVectorTemplate(td: TypeDeclPtr) : bool()
returns true if the given type declaration is a das::vector template bound on C++ side
- Arguments
td : TypeDeclPtr