Does Eiffel avoid ALL nulls?

392 Views Asked by At

I am actually using C# - but the question of Nulls came up on a local developers group (Chester Devs, UK) social site about the issues with nulls

An object Person has a property, say Name, of type String If the name is not known then in C# Name is null

Does Eiffel have a better way than C# ( if x is null ...) to deal with this common dynamic void ?

1

There are 1 best solutions below

1
Alexander Kogtenkov On

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.