Spec#, nullables and more

Roman Ivanov isroman.DEL at ETE.km.ru
Sat Nov 6 13:26:10 PDT 2010


On 11/6/2010 2:14 PM, Walter Bright wrote:
> Rainer Deyke wrote:
>> On 11/6/2010 02:42, Walter Bright wrote:
>>> Adam D. Ruppe wrote:
>>>> It wasn't until I added the invariant and in/out contracts to all the
>>>> functions
>>>> asserting about null that the problem's true cause became apparent.
>>> Couldn't this happen to you with any datum that has an unexpected value
>>> in it?
>>>
>>> Suppose, for example, you are appending the numbers 1..5 to the array,
>>> and somehow appended a 17. Many moons later, something crashes because
>>> the 17 was out of range.
>>
>> That's an argument for limited-range data types, not against
>> non-nullable types.
> 
> I see it more as an argument for any restricted subset of a data type.
> It could be that you want just odd numbers in the array, and an even one
> crept in.

Not quite. I don't want to make an argument in favor of general subsets
of data types. (IMO it can be made, but that's a topic of a different
discussion.) I just want to point out that nulls are a special case,
because they allow anyone to bypass (effortlessly, often unknowingly!)
the type checks enforced by class system, which is probably the most
important part of object-oriented programming.

The problem you outlined above can be solved by creating a class that
represents an odd number. But without unnulable types the representation
will never be precise, because any variables of that type could (in
addition to all the odd numbers) also be null.


More information about the Digitalmars-d mailing list