Generic Parameter Passing lazarus / 62-General ---------------------------------------------------------------------- Warfley · 2022-03-18 22:16 So here is a weird thing I was wondering about quite a while. Taking a generic class: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TTest = class(TObject);And you want to do a partial specialization, you can do the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TOneGenericParamSubclass = class(specialize TTwoGenericParams);But you can't do the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TOneGenericParam = specialize TTwoGenericParams; // Error Identifier not found "T" For classes this is annoying but nothing more as the subclass method can be used easiely, but for other types this is kinda a dealbreaker, e.g. for functions: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TUnaryFunction = function(const AValue: TParam): TResult;  generic TToStringFunction = specialize TUnaryFunction;Or for records: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TUnion = record    // ...  end;  generic TUnaryFunctionRaw = function(const AValue: TParam): TResult;  generic TUnaryFunctionMethod = function(const AValue: TParam): TResult of object;   generic TUnaryFunction =    specialize TUnion,                      specialize TUnaryFunctionMethod>; This behavior is in the fpc for a long time now, so my question is, is this intentional or a bug? I found this to be really annoying as it makes a lot of things simply not possible. And I do not see a workaround for this ---------------------------------------------------------------------- PascalDragon · 2022-03-19 15:58 Quote from: Warfley on March 18, 2022, 11:16:42 pmThis behavior is in the fpc for a long time now, so my question is, is this intentional or a bug? I found this to be really annoying as it makes a lot of things simply not possible. And I do not see a workaround for this FPC simply does not support the declaration of partial specializations (it implicitly supports them as you can see in the example where you descend a class from a partial specialization, but it provides no explicit way to use them). ---------------------------------------------------------------------- Warfley · 2022-03-19 17:58 So this is intendet? This also means that type aliases are also not possible with FPC? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TTest = class(TObject);  generic TAlias1 = specialize TTest;  generic TAlias2 = type specialize TTest;Or nested specializations: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TFoo = class(TObject);  generic TBar = class(TObject);  generic TFooBar = specialize TFoo>; I have found out that it is possible by just adding another layer of indirection: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TTest = class(TObject);  generic TestAlias = class public type  TTestAlias = specialize TTest;  TTestAliasNewType = type specialize TTest;  end; // usage:var  t1: specialize TestAlias.TAlias1;  t2: specialize TestAlias.TAlias2; Honestly, I find this is a rather odd choice, because type aliasing an essential part of Pascal and is required (especially when dealing with cross plattform code, just look at for example the sockets unit), so requiring programmers to use such workarounds forces them to write worse code ---------------------------------------------------------------------- PascalDragon · 2022-03-20 10:47 Quote from: Warfley on March 19, 2022, 06:58:33 pmSo this is intendet? I wouldn't say “intended”, but simply “not implemented”. The generics are a rather complex functionality in the compiler and there are many things that can go wrong when one changes something. Quote from: Warfley on March 19, 2022, 06:58:33 pmThis also means that type aliases are also not possible with FPC? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TTest = class(TObject);  generic TAlias1 = specialize TTest;  generic TAlias2 = type specialize TTest; Correct. Quote from: Warfley on March 19, 2022, 06:58:33 pmOr nested specializations: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TFoo = class(TObject);  generic TBar = class(TObject);  generic TFooBar = specialize TFoo>; Nested specializations are supported, but not as part of a construct like that. Quote from: Warfley on March 19, 2022, 06:58:33 pmHonestly, I find this is a rather odd choice, because type aliasing an essential part of Pascal and is required (especially when dealing with cross plattform code, just look at for example the sockets unit), so requiring programmers to use such workarounds forces them to write worse code This has nothing to do with “odd choice”, but simply with generics still being essentially “in development”. Many of the things that are possible today weren't possible with the original idea of generics (which were conceived before Delphi added theirs). If something turns out to be a useful functionality it will be implemented. The question is mainly when, cause as written above: generics are complex and prone to breakage and I have only so much time I can invest into working on FPC.