On Sun, 19 Aug 2012 14:08:17 +0200, bearophile <bearophileh...@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

Reply via email to