Parameterized Types

Claro supports the definition of types that may be parameterized with a generic type so that they can be used to contain arbitrary types. For example the following is a definition of a type Maybe<T> that has the generic type param T:

Fig 1:


newtype Maybe<T> : oneof<T, std::Error<std::Nothing>>

This type definition is useful for describing the generic concept of a value that may or may not be present, without needing to define repeated declarations for each specific type that may or may not be present:

Note: Claro's error messaging is a work in progress - the below error message will be improved.

Fig 2:


var maybeInt: Maybe<int> = Maybe(10);
print(maybeInt);

var maybeStr = Maybe("hi");
print(maybeStr);

Output:

Maybe(10)
Maybe(hi)

Generic Type Param Must be Referenced in Type Declaration

The generic type param must be referenced somewhere in the type definition or Claro will statically reject the definition with an explanation of the problem.

Fig 3:


newtype Invalid<X> : struct {row: int, col: int}

Compilation Errors:

Warning! The following declared symbols are unused! [X]
1 Error