Spec#, nullables and more

Simen kjaeraas simen.kjaras at gmail.com
Sat Nov 6 02:26:58 PDT 2010


Walter Bright <newshound2 at digitalmars.com> 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?

Yes. And by augmenting the type system (such as with non-nullable
pointers or bounded integers), such a mistake could be caught early,
likely at compile time, because the bounded type or non-null pointer does
not allow assignment from arbitrary pointers or values.


> 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.

Bounded!(int, 1, 5)[] myArr;
myArr ~= 1; // Compile-time error: int is not implicitly castable to  
Bounded!(int, 1, 5)
myArr ~= Bounded!(int, 1, 5)( 1 ); // Works perfectly


-- 
Simen


More information about the Digitalmars-d mailing list