Recent

Author Topic: Type check on type aliases  (Read 1016 times)

gidesa

  • Jr. Member
  • **
  • Posts: 67
Type check on type aliases
« on: January 24, 2024, 01:00:54 pm »
Hello,
I have this situation, with a type alias of integer:

Code: Pascal  [Select][+][-]
  1. type
  2.   MyInteger = type integer;  // note the "type" before "integer"
  3. var
  4.   mi: MyInteger;
  5.   i: integer;
  6. begin
  7.   mi:=i;
  8. end;
  9.  
As says the manual, MyInteger internally results as a different type from integer.
But the compiler accept the assignment without any error or warning.
Is possible instead to generate an error?
With that definition the programmer wants explicitly a new type, not a simple alias of integer.
All checks during compilation time are a great aid, in my opinion. And in this case FPC compiler could be
almost ready to add this check.
Thanks



« Last Edit: January 24, 2024, 03:33:22 pm by gidesa »

cdbc

  • Hero Member
  • *****
  • Posts: 1076
    • http://www.cdbc.dk
Re: Type check on type aliases
« Reply #1 on: January 24, 2024, 02:35:23 pm »
Hi
I think it's because they're value compatible, it is also possible to assign a word to a byte, just beware of the possible funny/wrong result  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

alpine

  • Hero Member
  • *****
  • Posts: 1060
Re: Type check on type aliases
« Reply #2 on: January 24, 2024, 03:10:34 pm »
Wrap it in a record:
Code: Pascal  [Select][+][-]
  1. type
  2.   MyInteger = record
  3.     I: Integer;
  4.   end;
  5.  
  6.   // Overload operators as needed
  7.   operator +(A,B: MyInteger) C: MyInteger;
  8.   begin
  9.     C.I := A.I + B.I;
  10.   end;  
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 14363
  • Sensorship about opinions does not belong here.
Re: Type check on type aliases
« Reply #3 on: January 24, 2024, 03:30:39 pm »
The specialized types are assignment compatible, but you can check if the types are equal.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

gidesa

  • Jr. Member
  • **
  • Posts: 67
Re: Type check on type aliases
« Reply #4 on: January 24, 2024, 03:32:43 pm »
Yes, I could use records, and I have tried in other cases.
And sure, at run time I can check types and more.
But it not resolve the issue of having checks at compile time, in my opinion is much better.
For example, a numeric type record with run time controls (Delphi syntax):

Code: Pascal  [Select][+][-]
  1. // integer bounded to 1-10 range
  2. type
  3.   integer1b10 = record
  4.   strict private
  5.     num: Integer;
  6.   public
  7.     class operator Implicit(const N: integer): integer1b10;
  8.     { Other operator as:
  9.     class operator Implicit(const N: integer1b10): integer;
  10.     class operator LessThan(N: integer1b10; I: integer): Boolean;
  11.     class operator Add(N: integer1b10; I: integer): integer1b10;
  12.     class operator Inc(N: integer1b10) : integer1b10;
  13.     ................
  14.     }
  15.   end;
  16.  
  17. class operator integer1b10.Implicit(const N: integer): integer1b10;
  18. begin
  19.   if (1<=N) and (N<=10) then
  20.     Result.num:=N
  21.   else
  22.     raise EOverflow.Create('integer1b10: out of 1-10 bounds');
  23. end;    
  24.  
  25. var
  26.   i10: integer1b10;
  27.  
  28. begin
  29.   i10:=20;  // no warning/error at compile time; generate exception at run time
  30. end;
  31.  

« Last Edit: January 24, 2024, 03:35:02 pm by gidesa »

alpine

  • Hero Member
  • *****
  • Posts: 1060
Re: Type check on type aliases
« Reply #5 on: January 24, 2024, 03:39:52 pm »
You asked for an assignment-incompatible type. You can't assign integer to it (compile time) unless you overload the assignment operator.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

gidesa

  • Jr. Member
  • **
  • Posts: 67
Re: Type check on type aliases
« Reply #6 on: January 24, 2024, 03:42:54 pm »
In the integer1b10 example, the assignment operator (from integer) is overloaded.
It's the class operator Implicit.
But no check at compile time.  :(

Thaddy

  • Hero Member
  • *****
  • Posts: 14363
  • Sensorship about opinions does not belong here.
Re: Type check on type aliases
« Reply #7 on: January 24, 2024, 03:49:07 pm »
Yes, I could use records, and I have tried in other cases.
Why? That is silly. type 1 is not equal to type 1 type 1 anyway.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

alpine

  • Hero Member
  • *****
  • Posts: 1060
Re: Type check on type aliases
« Reply #8 on: January 24, 2024, 03:54:28 pm »
In the integer1b10 example, the assignment operator (from integer) is overloaded.
It's the class operator Implicit.
But no check at compile time.  :(
Then do not overload Implicit(). Can you paraphrase the original question, please?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Joanna

  • Hero Member
  • *****
  • Posts: 755
Re: Type check on type aliases
« Reply #9 on: January 24, 2024, 04:11:49 pm »
I’m not sure what the purpose of trying to create a new type of integer would be. Of course a type of integer would be an integer.  This reminds me of ctk...
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

gidesa

  • Jr. Member
  • **
  • Posts: 67
Re: Type check on type aliases
« Reply #10 on: January 24, 2024, 04:37:28 pm »
Briefly, from FPC reference guide:

Quote
"
3.8  Type aliases
-------------------

............

Type
MyInteger = Type Integer;
This not only creates a new name to refer to the Integer type, but actually creates a new type. That
is, two variables:
Var
A : MyInteger;
B : Integer;
Will not have the same type from the point of view of the compiler. However, these two types will
be assignment compatible. That means that an assignment
A:=B;
will work.
The difference can be seen when examining type information:
    If TypeInfo(MyInteger)<>TypeInfo(Integer) then
                        Writeln('MyInteger and Integer are different types');
"


My question: would be possible, modifying the compiler, to have a warning or error AT COMPILE TIME, instead that the silent assigment compatibility? Maybe with a compiler option/switch.
As Joanna and Thaddy observe,  why to simply assign an alias to an integer (and, indeed, to every other type, native or user) ? The manual well explains situations where this is useful.
Maybe FPC developers do not plan to add this compile check. Whatever is the answer, FPC remains a great compiler for me.
Thanks




Joanna

  • Hero Member
  • *****
  • Posts: 755
Re: Type check on type aliases
« Reply #11 on: January 24, 2024, 04:41:54 pm »
I don’t think they will change it, this has been thoroughly discussed on a previous occasion
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

 

TinyPortal © 2005-2018