Forum > FPC development
Feature request: hard type creation/declaration
440bx:
--- Quote from: Martin_fr on July 08, 2025, 05:15:38 pm ---Btw, if you only need one base type, then you don't even need the generic.
--- End quote ---
Can you elaborate on that ?... the majority of handles are ptruint (in Windows)...
Your solution depends on defining records, which works but it really isn't desirable... but that solves the "type" problem...
Martin_fr:
--- Quote from: 440bx on July 08, 2025, 05:27:22 pm ---
--- Quote from: Martin_fr on July 08, 2025, 05:15:38 pm ---Btw, if you only need one base type, then you don't even need the generic.
--- End quote ---
Can you elaborate on that ?... the majority of handles are ptruint (in Windows)...
Your solution depends on defining records, which works but it really isn't desirable... but that solves the "type" problem...
--- End quote ---
No it doesn't solve the type problem.
If you only need Ptruint, then you can define your "record wrapper" as a normal record instead of as a generic.
But you still have do then do
--- 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";}};} ---TMFooHandle = type TMWrapper;
And that still has the issue of not copying all operators.
Mind that with the generic, even though you have a specialize on each declaration, that does not create new types.
--- 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";}};} ---var a: specialize TFoo<byte>; b: specialize TFoo<byte>;gives the same type to a and b.
The 2nd specialize re-uses the same type that already exists. That is why you can specialize inline, even do stuff like (if TFoo was a class): begin a := specialize TFoo<byte>.create;
However since it is just the explicit operator that does not get copied....
You can declare the := operator. That seems to get copied.
--- 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";}};} ---class operator :=(const i: integer): TTypeWrapper;
That also allows typecasts.
Just unfortunately it also allows
TFooHandle := 0; // without typecast.
but different handles are still incompatible.
440bx:
Martin, thank you for elaborating.
Warfley:
Quick update on the matter, I found the issue and I've fixed it (tbh quite a horrible fix but seems to work). The following test case now compiles:
--- 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";}};} ---{$mode objfpc}{$modeswitch advancedrecords}type generic TTypeWrapper<T> = record data: T; class operator =(const lhs, rhs: specialize TTypeWrapper<T>): Boolean;inline; class operator <>(const lhs, rhs: specialize TTypeWrapper<T>): Boolean;inline; class operator Explicit(const rec: specialize TTypeWrapper<T>): T;inline; class operator Explicit(const value: T): specialize TTypeWrapper<T>;inline; end; class operator TTypeWrapper.=(const lhs, rhs: specialize TTypeWrapper<T>): Boolean;begin result:=lhs.data=rhs.data;end; class operator TTypeWrapper.<>(const lhs, rhs: specialize TTypeWrapper<T>): Boolean;begin result:=lhs.data<>rhs.data;end; class operator TTypeWrapper.Explicit(const rec: specialize TTypeWrapper<T>): T;begin result:=rec.data;end; class operator TTypeWrapper.Explicit(const value: T): specialize TTypeWrapper<T >;begin result.data:=value;end; type TIntegerWrapper = specialize TTypeWrapper<Integer>; THWND = type TIntegerWrapper; THandle = type TIntegerWrapper; var hw: THWND; hnd: THandle;begin hw := THWND(0); hnd := THandle(-1); if Integer(hw) <> 0 then Halt(1); if Integer(hnd) <> -1 then Halt(2); if not (hw=hw) then Halt(3); if hw<>hw then Halt(4);end.
Pipeline still failing and need to test a bit more, but hopefully I get this done by the weekend
Navigation
[0] Message Index
[*] Previous page