NaNs Just Don't Get No Respect

Simen Kjaeraas simen.kjaras at gmail.com
Sun Aug 19 14:53:28 PDT 2012


On Sun, 19 Aug 2012 14:08:17 +0200, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Walter Bright:
>
>> Oh come on. That's called a "user defined type."
>
> This D code compiles and it throws an "Enforcement failed" Exception at  
> runtime:
>
> import std.typecons: Nullable;
> void main() {
>      Nullable!int x;
>      int y = x;
> }
>
>
> With a different type system the compiler makes sure at compile-time  
> that x is not empty (this means the compiler makes sure in no code paths  
> x is used before testing it contains something), avoiding the run-time  
> exception.

We could define a Nullable!T that does not allow access to the payload
value, and only give that access inside a special function, like
pattern matching in other languages:

Nullable!int a;

int n = a.match!(
     (int x)  => x,
     (None n) => 0
);

-- 
Simen


More information about the Digitalmars-d-announce mailing list