Concrete Type Inference

Claro is able to automatically infer the type of maybeStr whenever it would be possible to do so. Generally speaking, this is possible when the concrete type is actually referenced in the initialization.

Fig 1:


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

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

Output:

Maybe<string>
Maybe(hi)

It's not always possible to automatically infer the type of an instance of a parameterized type. In particular, the below example is impossible to automatically infer as the concrete type is not actually referenced in the initialization:

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

Fig 2:


var maybeStr = Maybe(std::Error(std::Nothing));
print(maybeStr);

Compilation Errors:

concrete_type_inference_EX2_example.claro:1: Invalid Generic Procedure Call: For the call to the following generic procedure `Maybe$constructor` with the following signature:
		`function<oneof<T, [module at //stdlib:std]::Error<Nothing>> -> Maybe<T>> Generic Over {T} Requiring Impls for Contracts {}`
	The output types cannot be fully inferred by the argument types alone. The output type must be contextually constrained by either a type annotation or a static cast.
var maybeStr = Maybe(std::Error(std::Nothing));
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1 Error

In these situations Claro will require you to provide an explicit type annotation to disambiguate your intentions:

Fig 3:


var maybeStr: Maybe<string> = Maybe(std::Error(std::Nothing));
print(maybeStr);

Output:

Maybe(Error(Nothing))