Forum > FPC development
Feature request: hard type creation/declaration
gidesa:
Hello,
the topic was discussed in slightly different mode in
https://forum.lazarus.freepascal.org/index.php/topic,65981.
As of today, there is some usefulness of that syntax, although in very limited cases, described in FPC manual 3.8 paragraph.
Thaddy:
Another example where the types are distinct:
--- 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} {$H+}{$modeswitch typehelpers}type Metres = type double; Kilometres = type double;type TMetreHelper = type Helper for Metres function ToKilometres:Kilometres; end; TKiloMetreHelper = type helper for Kilometres function ToMetres:Metres; end; function TMetreHelper.ToKilometres:Kilometres; begin result := self / 1000; end; function TKilometreHelper.ToMetres:Metres; begin result := self * 1000; end; var m1: Metres = 500.0; km1: Kilometres = 5.0; begin writeln( km1 + m1.ToKilometres:4:2); writeln( km1.ToMetres + m1:4:2); readln;end.The type helpers are strictly bound to the types: the type helper for metres is not accessible for kilometres and vice versa.
You don't need a record for that.
MarkMLl:
--- Quote from: Thaddy on July 07, 2025, 12:49:45 pm ---Another example where the types are distinct:
...
The type helpers are strictly bound to the types.
--- End quote ---
But when various people investigated this a few weeks ago it was found that underlying arithmetic wasn't, so even if helpers and overloads can be kept distinct there is a massive underlying trap for the unwary... and even for the wary.
MarkMLl
440bx:
--- Quote from: Thaddy on July 07, 2025, 12:07:13 pm ---Basically it does create a new type, but the types remain otherwise assignment compatible.
--- End quote ---
if it were a new type it wouldn't be assignment compatible, e.g, all records are new types and they are definitely not assignment compatible.
Currently there are convoluted ways to sort of implement new types but, they cause a lot of hassles. They simply are not what a new type should be.
As I stated in the OP, I am aware that this is not the first time this feature has been suggested'/requested/discussed but, this is really a basic feature that should be implemented sooner rather than later (actually, it should already be implemented.)
Martin_fr:
--- Quote from: 440bx on July 07, 2025, 01:02:21 pm ---
--- Quote from: Thaddy on July 07, 2025, 12:07:13 pm ---Basically it does create a new type, but the types remain otherwise assignment compatible.
--- End quote ---
if it were a new type it wouldn't be assignment compatible, e.g, all records are new types and they are definitely not assignment compatible.
--- End quote ---
Exactly, so
--- 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";}};} ---type HWND: record val: THandle; end; HFont: record val: THandle; end;
Will do.
Of course, they are not assignment compatible, so any existing function declaration needs to be changed to use those types.
You can define operators for assignments you want to allow.
Navigation
[0] Message Index
[#] Next page
[*] Previous page