Forum > FPC development

Feature request: hard type creation/declaration

(1/13) > >>

440bx:
Hello,

One of the possible problems when programming is passing a handle to the wrong thing to a function/procedure/method and the compiler is unable to catch this error because, as of now, most handles are assignment compatible with one another.

For instance, in Windows, an HWND can be assigned to an HMENU which can be assigned to an HPALETTE which can be assigned to an HBITMAP and so on.   This is because these types are not real types independent of each other but, synonyms of their underlying type (which is usually the same for most, if not all, handles.)

The request is simple: provide a way to declare types that even though may share the same underlying type are NOT assignment compatible with one another.  IOW, provide a simple and effective way to ensure an HWND cannot be assigned to an HBITMAP or anything else that is not an HWND (however, still allow typecasting.)

This would help prevent bugs and would set FPC apart.  It is a very, very simple feature but, it would really be a very nice feature to have.

I believe I have requested this feature in the past, I also believe I am not the only one to request it (using other words but, same concept.)  I believe the feature is important enough that it should be requested again.

Maybe if enough people support the addition of this feature, it might eventually make it into the compiler (of course, the sooner, the better.)

Comments welcome.

ETA:

Added poll

marcov:
The syntax exist in pascal, but the Borland derived dialects lack in enforcing it.


--- 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    xx = type yyy; 
IIRC the description is something like "create a separate entry in the type symbol table for xx", in other words, "don't alias".

I'm no expert of the typesystem, but I assume it won't be a trivial {$modeswitch }, it will be harder to reconcile this with other typing related enhancements like the pointer type ( that is assignable from every pointer type), and ordinals, and overloads.

Personally I think this is a better avenue for a language improvement trajectory than the C operators and similar features that are mostly (shorthand) syntax only.

440bx:

--- Quote from: marcov on July 07, 2025, 11:24:56 am ---
--- 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    xx = type yyy; 
IIRC the description is something like "create a separate entry in the type symbol table for xx", in other words, "don't alias".

--- End quote ---
I know what you mean.  The "type.... type" is _supposed_ to create a new type but, it doesn't.

As far as implementation, what comes to mind is for the compiler to consider the type as if it were defined as a record (internally it could be a record.)  That would automatically make it assignment incompatible with other types defined that way.  There would be one additional thing needed, those "records" should allow a short hand form of typecasting that would not be available with normal records, this because it would be a "field-less" record. 

That would allow assigning values of the underlying type, e.g, NEWTYPE = NEWTYPE(0);  which is something the compiler already does when assigning ordinal values to enumeration types.

Long story long already, I believe it might not take a lot of work to implement the feature because it could be mostly implemented using characteristics that are already part of the language and, the additional stuff seems to be isolated to when the type is declared.

Of course, implementation is something secondary.  First, there has to be agreement to adding the feature into the compiler.

Thaddy:
The type system is mainly to allow overloads.
An example:
--- 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+} type  MyString = type AnsiString;    procedure printme(const s:MyString);overload;  begin    write('Type system uses MyString: ');    writeln(s);  end;   procedure printme(const s:AnsiString);overload;  begin    write('Type system uses AnsiString: ');    writeln(s);  end; var  a:AnsiString = 'AnsiString';  b:MyString = 'MyString';begin  printme(a);  printme(b);end.I believe this is documented somewhere.
Basically it does create a new type, but the types remain otherwise assignment compatible.

MarkMLl:

--- Quote from: 440bx on July 07, 2025, 11:45:28 am ---
--- Quote from: marcov on July 07, 2025, 11:24:56 am ---IIRC the description is something like "create a separate entry in the type symbol table for xx", in other words, "don't alias".

--- End quote ---
I know what you mean.  The "type.... type" is _supposed_ to create a new type but, it doesn't.

--- End quote ---

Xref to older thread https://forum.lazarus.freepascal.org/index.php/topic,70802.0.html

The upshot of that was that it should be possible to declare a hard type using a few dozen lines of generics, but this is at present only implemented in main.

I suggest that this be elevated to a request to get this into the compiler no later than 3.4

MarkMLl

Navigation

[0] Message Index

[#] Next page

Go to full version