I’m working on creating / extending new syntax in a preprocessor for standard Delphi.
Matching would be exhaustive, so something like …
match N of
| 1, 3, 5, 7, 9 : 'odd'
| 0, 2, 4, 6, 8 : 'even'
| _ : 'out of range' // Leaving out | _: on an Integer is a compile error.
end
In the output, the _ arm becomes the case’s ordinary else.
case N of
1, 3, 5, 7, 9: Result := 'odd';
0, 2, 4, 6, 8: Result := 'even';
else
Result := 'out of range';
end;
and for enums (the first proposal) …
type
TTxn =
| Cheque(Number: Integer; Drawee: TBankId)
| Wire ( Swift: TSwiftCode)
| Cash
| Card ( Last4: TDigits4; Auth: TAuthCode);
function Describe(const T: TTxn): string;
begin
Exit( match T of
| Cheque(No, Bank) : Format('cheque %d on %s', [No, Bank])
| Wire(Code) : 'wire via ' + Code
| Cash : 'cash'
| Card(Last4, _) : 'card ending ' + Last4
end
);
end;
translated to …
procedure __dpp_Unreachable; noreturn;
begin
raise EInvalidOpException.Create('unreachable');
end;
function Describe(const T: TTxn): string;
begin
case T.Kind of
tkCheque: Exit( Format('cheque %d on %s', [T.Number, T.Drawee]) );
tkWire: Exit( 'wire via ' + T.Swift );
tkCash: Exit( 'cash' );
tkCard: Exit( 'card ending ' + T.Last4 );
else
__dpp_Unreachable;
end;