[fpc-devel] Implicit function specialization precedence

Ryan Joseph genericptr at gmail.com
Thu May 6 17:33:01 CEST 2021


I found something sneaky I'd like to confirm before I decide what to do about it.

1) "T" in TAnyClass<T> is specialized as Integer from the first parameter with TSomeClass (which is TAnyClass<Integer>).
2) "U" is getting specialized as String by looking at the parameters in Compare() in which "U"(the second generic parameter) is String.

This specializes the procedure correctly but it uses a very sneaky method which is very hard to discern. I feel like that if a generic parameter is already used (like T in specialize TCallback<T, U>) then no further attempt should be made to look at the parameters and in the example below "U" would not be found and the function would fail to implicitly specialize.

==============================

type
  generic TAnyClass<U> = class
    type TElem = U;
  end;

type
  TSomeClass = specialize TAnyClass<Integer>;

type
  generic TCallback<T, U> = function(a: T; b: U): integer;

function Compare(a: TSomeClass.TElem; b: string): integer;
begin
  result := 1;
end;

generic procedure DoThis<T, U>(aClass: specialize TAnyClass<T>; callback: specialize TCallback<T, U>);
begin
  callback(1, 'string');
end;

begin
  DoThis(TSomeClass.Create, @Compare);
end.

Regards,
	Ryan Joseph



More information about the fpc-devel mailing list