[fpc-pascal] Feature announcement: Generic functions, procedures and methods

Anthony Walter sysrpl at gmail.com
Mon Nov 23 01:20:35 CET 2015


Sven, in the with regards to "of course", I think you have it reversed. Is
and as were being used as a class in my test, not an object.

All examples in {$mode delphi}

if Form1.Components[I] is T then // does not compile in my test

while

if Form1.Components[I].Inheritsfrom(T) // did compile

In essence, I could not use "is" or "as". Here is a test:

function TestIsAs<T>(Item: TObject): T;
begin
  if Item is T then
  begin
    Exit(Item as T);
  end;
  Result := nil;
end;

Result - Error: Class or interface type expected, but got "T" (on line "if
Item<error here> is T then")

With regards to constraints, I tried them previously, but went back to test
them again realized that with class methods the constraint must be in the
method declaration of the class only.

That is:

type
  TConstraintMethodTest = class
  public
    procedure SomeTest<T: TComponent>(Value: T);
  end;

// Below is wrong
procedure TConstraintMethodTest.SomeTest<T: TComponent>(Value: T);
begin
end;

// This works
procedure TConstraintMethodTest.SomeTest<T>(Value: T);
begin
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151122/e40b5011/attachment.html>


More information about the fpc-pascal mailing list