Eiffel allows for void
values (null
in C#). However it makes sure there is never a call on a void target (i.e., there is no NullReferenceException
). This is ensured at compile time by relying on the type system that is augmented with attached
/detachable
notion of a type and on a set of special void-safety rules that guarantee that any expression of an attached type is always attached to an object at run-time (i.e. is never null
).
In your example, the class declaration will look like
class PERSON ... feature
name: detachable STRING
end
Then in the code it can be used as
p: PERSON
a: STRING
d: detachable STRING
...
d := p.name -- OK
a := p.name -- This is not allowed, because `a' is of an attached type.
if attached p.name as q then
a := q -- OK
... -- Both `q' and `a' are equal to `p.name' and are attached.
else
... -- The name is `void', do something else.
end
It might be possible to have an OPTION
type and rely on it when some value may be present or absent, but absence of a value is naturally represented by void
, this is what it is designed for, so usually there is little need for a special type.