Safe casts now implemented

I’m happy to announce that the safe casts are finally implemented by the compiler.

The syntax is not what I planned, because I decided not to be insane. Here is the sample program:

def main() -> S32 {
    def var : S8 = expect!S16(0-128);
    def var2 : S8 = expect!S16(1-129);

    var
}

0-128 wouldn’t compile without the safe cast because 128 is not a valid S8 literal. By telling it to expect an S16, we allow the expression 0-128 to fit into the type.

Once it is calculated, however, value range knows it’s value is actually -128, which does fit into a S8. The implicit cast from S16 to S8 happens without any further intervention on our part.

The second line is more of the same, just testing an even larger range.

I am using C++'s cast syntax of cast-name(template-syntax-type)(expression), but D’s template syntax of using ! as a binary operator. I think this takes the good parts from both languages, but I am, as always, open to hear contrary opinions if you have them.

Shachar