References
References provide a way to share data between instances and to define recursive data types that would otherwise be infinitely large.
A List using references
Here is a small example that defines a feature Node
that contains
a String
and an optional tail
that is itself
a Node
or nil
.
When trying to compile this, we get an error due to cyclic types:
A Node
may not contain itself, so the object layout fails.
A solution is to declare Node
to be a ref
, i.e., any
field of type Node
will not contain a Node
directly, but only refer to a Node
. Now the code compiles and runs:
Dynamic Binding
TBW!
A reference value of a given feature type f
can be assigned to a
reference field whose type is a reference to f
or, recursively, any
of features called in f
's inherits clause.
Assigning value instances to references
TBW!
This should automatically create a reference to the instance (if it is immutable and has a lifespan that exceeds that of the reference) or a copy of the instance.
Assigning reference to value fields
TBW!
It must be decided if this should be allowed at all, my preference is to forbid it since the dynamic binding gets lost and the resulting behavior may be wrong.
null Reference
There is no null
-reference in Fuzion, it
avoids the billion-dollar
mistake similar to F#
or Rust by
using a choice
type with nil
representing the absence of a reference.