Define
The “def” statement
The def
statement in C3 is intended for aliasing identifiers and types.
Defining a type alias
def <type alias> = <type>
creates a type alias. Type aliases need to follow the name convention of user defined types (i.e. capitalized
names with at least one lower case letter).
Function pointers must be aliased in C3. The syntax is somewhat different from C:
This defines an alias to function pointer type of a function that returns nothing and requires two arguments: an int and a bool. Here is a sample usage:
Distinct types
Similar to def
aliases are distinct
which create distinct new types. Unlike type aliases,
they do not implicitly convert to or from any other type.
Literals will convert to the distinct types if they would convert to the underlying type.
Because a distinct type is a standalone type, it can have its own methods, like any other user-defined type.
Distinct inline
When interacting with various APIs it is sometimes desirable for distinct types to implicitly convert to its base type, but not from that type.
Behaviour here is analogous how structs may use inline
to create struct subtypes.
Function and variable aliases
def
can also be used to create aliases for functions and variables.
The syntax is def <alias> = <original identifier>
.
Using def
to create generic types, functions and variables
It is recommended to favour using def to create aliases for parameterized types, but it can also be used for parameterized functions and variables:
For more information, see the chapter on generics.
Function pointer default arguments and named parameters
It is possible to attach default arguments to function pointer aliases. There is no requirement that the function has the same default arguments. In fact, the function pointer may have default arguments where the function doesn’t have it and vice-versa. Calling the function directly will then use the function’s default arguments, and calling through the function pointer will yield the function pointer alias’ default argument.
Similarly, named parameter arguments follow the alias definition when calling through the function pointer: