2

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 ?

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • 1
    C# has a better way to deal with `null` too; `Optional`/`Maybe` like types, the Null-object pattern etc. They're not widely used of course, but they exist. You can do the similar things in Eiffel, although I don't know enough about it to tell you which particular option they prefer. – Cubic Feb 09 '16 at 11:58
  • @Cubic Could you precise the C# solution to deal with null? – Emmanuel Stapf Feb 10 '16 at 12:10
  • Not in the scope of a comment. Just look up the terms I outlined in the previous comments, you're bound to find _something_ useful. – Cubic Feb 10 '16 at 12:15

1 Answers1

2

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.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Thanks. This pushes the problem of Null at run-time to wherever the property values are coming from. ( Typically a database) TBW Which raises the related ( but not on-topic) issue of why most databases allow null.. – Andrew Bingham Feb 10 '16 at 12:46