Boundaries
boundaries are a feature of int and float types
they allow you to specify a range for numerical values
syntax
int<start..end>
float<start..end>
start is the minimum allowed value.
end is the maximum allowed value.
example
egg age int<0..120> = 25
egg temperature float<-273.15..10000>
Here:
agecannot be less than 0 or greater than 120temperaturecannot go below absolute zero
runtime behavior
ff a value is assigned outside the boundary range, the program will produce an error.
Example:
age int<0..120> = 200
output:
runtime error at 1:4: value 200 above maximum 120
using boundaries in function parameters
fun setVolume(level int<0..100>) {
putln(level)
}
this guarantees the function only receives valid values
notes
- boundaries work with both
intandfloat - the range is inclusive, meaning the
startandendvalues are allowed - boundaries are checked when assigning values