Forum > Free Pascal

Feature announcement: Implicit generic function specializations

(1/4) > >>

PascalDragon:
Dear FPC community,

The FPC developers are pleased to announce the implementation of a new feature: implicit generic function specializations. This feature was implemented by Ryan Joseph, so thank you very much, Ryan.

This feature allows you to use generic routines (functions, procedures, methods) without explicitely specializing them (“<…>” in Delphi modes and “specialize …<…>” in non-Delphi modes) as long as the compiler can determine the correct parameter types for the generic.

This feature is enabled with the modeswitch ImplicitFunctionSpecialization and is for now not enabled by default as this has the potential to break existing code.

Assume you have the following function:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---generic function Add<T>(aArg1, aArg2: T): T;begin  Result := aArg1 + aArg2;end;
Up to now you could only use this function as follows:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SomeStr := specialize Add<String>('Hello', 'World');SomeInt := specialize Add<LongInt>(2, 5);
However with implicit function specializations enabled you can also use it as follows:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SomeStr := Add('Hello', 'World');SomeInt := Add(2, 5);
The compiler will automatically determine the type of the generic parameters based on the parameters you pass in (this is always done left to right). Depending on the passed in parameters (especially if you're using constant values like in the example instead of variables) the compiler might however pick a different type than you expected. You can enforce a specific type by either explicitely specializing the method as before or by inserting a type cast. In the example above the compile will specialize the call with the parameters “2, 5” using an 8-bit signed type (Pascal prefers signed types) instead of a LongInt as in the explicit specialization. If you use “LongInt(2), 5” as parameters then the compiler will pick that instead, however with “2, LongInt(5)” it will still pick an 8-bit type, because the parameter types are determined left to right.

If there exists a non-generic overload for which the parameters types match exactly, the compiler will pick that instead of specializing something anew. So assume you also have the following function in scope:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Add(aArg1, aArg2: LongInt): LongInt;begin  Result := aArg1 + aArg2;end;
In the case of “Add(2, 5)” the compiler will not pick the non-generic function, because it determines that an 8-bit type is enough, however if you use “Add(LongInt(2), 5)” the compiler will pick the non-generic function.

Aside from simple parameters the compiler also supports arrays and function/method variables:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---generic function ArrayFunc<T>(aArg: specialize TArray<T>): T;var  e: T;begin  Result := Default(T);  for e in aArg do    Result := Result + e;end; type  generic TTest<T> = function(aArg: T): T; generic function Apply<T>(aFunc: specialize TTest<T>; aArg: T): T;begin  Result := aFunc(aArg);end; function StrFunc(aArg: String): String;begin  Result := UpCase(aArg);end; function NegFunc(aArg: LongInt): LongInt;begin  Result := - aArg;end; begin  Writeln(ArrayFunc([1, 2, 3])); // will write 6  Writeln(ArrayFunc(['Hello', 'FPC', 'World'])); // will write HelloFPCWorld   Writeln(Apply(@StrFunc, 'Foobar')); // will write FOOBAR  Writeln(Apply(@NegFunc, 42)); // will write -42end.
There are of course a few restrictions for this feature:

* all generic parameters must be used in the declaration of the routine (implementation only type parameters are not allowed)
* all parameters that have a generic type must not be default parameters, they need to be used in the call or their type must have been fixed by a parameter further left (as currently default values for parameters of a generic type are not supported this is not much of a restriction, but should that change (e.g. Default(T)) then this restriction will apply)
* the generic routine must not have constant generic parameters (this might be extended in the future with e.g. static arrays or file types, but for now this restriction stands)
* the result type is not taken into account, so if only the result type of a routine is generic then an implicit specialization does not work either
* function/method pointers to implicit specializations are not yet supported (pointers to explicit specializations are not yet supported either; once this changes the former will change as well)
* the compiler will silently discard generic functions that it can't specialize the declaration of; however if the declaration can be specialized correctly, but for whatever reason the implementation can not then this will trigger a compilation error
This feature is by and in itself Delphi-compatible however there might be differences in what FPC can implicitely specialize and what Delphi can. Especially if Delphi can specialize something that FPC can not, this should be reported.

mas steindorff:
this sounds like it's just the ticket for what I'm working with. 
What version /package / ?? do I need to download to access these features?

Zaher:
I love generics

Okoba:
Thank you Sven and specially Ryan. This is a fantastic upgrade.

Okoba:
If I have two overloaded functions in a unit, FPC detects the the correct one but if I split them in two units, it will raise an error:

--- Quote ---project1.lpr(22,8) Error: Wrong number of parameters specified for call to "Test"
--- End quote ---
Before there was not any error, but now, even without the ImplicitFunctionSpecialization mode switch, there is this error.
Version I am testing: Lazarus 2.3.0 (rev main-2_3-1668-g34b3b9a49a) FPC 3.3.1 x86_64-win64-win32/win64

This code works:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode Delphi}   function Test(const AString: Rawbytestring): Integer; overload;  begin   end;   function Test<T>(const AList: TArray<T>): Integer; overload;  begin   end; var  S: Rawbytestring;  I: Integer;begin  I := Test(S);end.                    
This raise the error:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode Delphi} interface function Test(const AString: Rawbytestring): Integer; overload; implementation function Test(const AString: Rawbytestring): Integer;begin end; end. 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit2; {$mode Delphi} interface function Test<T>(const AList: TArray<T>): Integer; overload; implementation function Test<T>(const AList: TArray<T>): Integer;begin end; end. 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode Delphi} uses unit1, unit2; var  S: Rawbytestring;  I: Integer;begin  I := Test(S);end.               

Navigation

[0] Message Index

[#] Next page

Go to full version