-
Notifications
You must be signed in to change notification settings - Fork 46
Constraint Usage
Iltotore edited this page Jun 24, 2021
·
17 revisions
This page informs on how to use Iron's constraints.
You can attach a constraint to:
- Variables
- Methods (Return type)
- Parameters
using the type Constrained[A, B]
or its alias ==>[A, B]
:
inline def log(x: Double ==> Greater[0d]): Double = Math.log(x)
You can also use type aliases:
type >[A, V] = A ==> Greater[V]
inline def log(x: Double > 0d): Double = Math.log(x)
Note: You need to have a suitable constraint behaviour in the implicit scope.
A constraint is evaluated at compile time if the value and the behaviour is fully inline. Otherwise, the compiler will inline as much as possible the constraint then evaluate at runtime. Runtime evaluation is disabled by default but can be useful for dynamic values.
The fallback behaviour can be configured using the -Diron.fallback
argument:
- error (default): Throw a compile time error
- warn: Warn, then fallback to runtime
- allow: Silently fallback to runtime