Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program IfThenElse_Proposal;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- type
- Generics = record
- class function IfThenElse<T>(condition: Boolean;
- trueCase, falseCase: TFunc<T>): T; static; inline;
- end;
- class function Generics.IfThenElse<T>(condition: Boolean; trueCase,
- falseCase: TFunc<T>): T;
- begin
- if condition then
- Result := trueCase
- else
- Result := falseCase;
- end;
- procedure Main;
- var
- a, b, c: Integer;
- begin
- a := 10;
- b := 0;
- c := Generics.IfThenElse<Integer>(b <> 0,
- function: Integer
- begin
- Result := a div b
- end,
- function: Integer
- begin
- Result := 0;
- end);
- (* Short syntax:
- c := IfThenElse(b <> 0, a div b, 0);
- // What do we need?
- // 1. standalone generic functions without the need to define them
- // inside a static class (or record)
- // 2. return type inference. c is of type Integer so it does not need to
- // be specified explicitly and it now can construct the TFunc<T> arguments
- // 3. implicit assignment compatibility of an expression to a TFunc<T> (or
- // lazy of T if you want a new language construct for it to distinguish better)
- *)
- end;
- begin
- try
- Main;
- except
- on E: Exception do
- Writeln(E.ClassName, ': ', E.Message);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement