Maybe
n.errors := case n.lookupName of
| nothing() -> ["error!"]
| just(_) -> []
end;
Silver does not have ‘null’ values, unlike languages such as Java or
C++, as these are a constant source of errors. Instead,
if one wishes for a value to potentially be absent, that must be reflected in its
type. The Maybe
type accomplishes this.
The type is written Maybe<a>
where a
is a type.
The constructors of this type are:
abstract production just
top::Maybe<a> ::= v::a
abstract production nothing
top::Maybe<a> ::=
where nothing
represents null, and just
is used otherwise.
Information is extracted from a Maybe using two attributes. isJust
determines whether there is information present or not. fromJust
returns
the value wrapped up inside the Maybe, assuming it is a just. (Otherwise, an
unrecoverable error occurs, similar to asking for the head of nil.)
Example:
function fromMaybe
a ::= otherwise::a ifJust::Maybe<a>
{
return if ifJust.isJust
then ifJust.fromJust
else otherwise;
}
This function will return the wrapped value from a Maybe if it is present, and if not, will return the default value specified.
The above example is actually a standard function for operating on Maybes.