[fpc-devel] [Suggestion] Enumeration range-check intrinsic

Ben Grasset operator97 at gmail.com
Tue Jul 16 23:55:59 CEST 2019


On Tue, Jul 16, 2019 at 8:35 AM Michael Van Canneyt <michael at freepascal.org>
wrote:

> 2. The Low(T) does not work, as there is no way to tell Delphi that T must
> be an enum.
>

Hmm, I guess it makes sense Delphi wouldn't allow the "Low", based on other
Delphi generics behavior (more like C#, whereas FPC is more like C++.)

I only realized recently that you can't even actually use "arithmetic"
operators on Delphi generics at all, E.G, the following is valid FPC code,
but not valid Delphi code:

program Example;

{$mode Delphi}

type
  TComparer<T> = record
  public
    class function Min(const A, B: T): T; static; inline;
    class function Max(const A, B: T): T; static; inline;
  end;

  class function TComparer<T>.Min(const A, B: T): T;
  begin
    if A < B then
      Result := A
    else
      Result := B;
  end;

  class function TComparer<T>.Max(const A, B: T): T;
  begin
    if A > B then
      Result := A
    else
      Result := B;
  end;

begin
  WriteLn(TComparer<Single>.Min(2.0, 4.0));
  WriteLn(TComparer<Single>.Max(2.0, 4.0));
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190716/d7c64dc4/attachment-0001.html>


More information about the fpc-devel mailing list