strict type checking lazarus / 62-General ---------------------------------------------------------------------- gbl · 2022-02-06 07:07 Hello, how do I implement a strict type checking for fundamental types? Example: 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 program type check; type tA = byte;  // for example. Both types are the same and cannot changedtype tB = byte; var A: tA;      B: tB;begin   A := B;   // this should not possible!   A := tA(B);   // this is OK.end.  sincerly ---------------------------------------------------------------------- Thaddy · 2022-02-06 07:39 Declare one as char: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 program typecheck; type tA = ansichar;type tB = byte; var A: tA;      B: tB;begin   A := B;   // this is not possible!   A := tA(B);   // this is OK.end. ---------------------------------------------------------------------- gbl · 2022-02-06 07:49 Thank you. Changing the types are not possible in my project. In this example I used byte. You can it change to string or dword. The goal is forcing the compiler to prevent implicit type casting. How can I accomplish this? THX ---------------------------------------------------------------------- Thaddy · 2022-02-06 08:23 Well, examine the manual on type aliases. https://www.freepascal.org/docs-html/current/ref/ref.html#QQ2-49-76 In essence, the assignment compatibility stays, but the type info on the two types will differ.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 uses typinfo;type   tA = type byte;  // type aliases  tB = type byte; var   Info:PTypeInfo;begin   Info := TypeInfo(Ta);   writeln(info^.Name,' ',info^.kind);   Info := TypeInfo(Tb);   writeln(info^.Name,' ',info^.kind);end.On some but not all types you can overload the assignment operators with this info in mind. ---------------------------------------------------------------------- ASerge · 2022-02-06 10:51 Quote from: gbl on February 06, 2022, 08:07:51 amhow do I implement a strict type checking for fundamental types? 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 ...   A := B;   // this should not possible!   A := tA(B);   // this is OK.end. 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 {$MODE OBJFPC}{$MODESWITCH ADVANCEDRECORDS} type  TByte1 = record    B: Byte;    class operator := (R: TByte1): Byte; inline;    class operator := (AB: Byte): TByte1; inline;  end;   TByte2 = record    B: Byte;    class operator := (R: TByte2): Byte; inline;    class operator := (AB: Byte): TByte2; inline;  end; class operator TByte1. := (AB: Byte): TByte1;begin  Result.B := AB;end; class operator TByte1. := (R: TByte1): Byte;begin  Result := R.B;end; class operator TByte2. := (R: TByte2): Byte;begin  Result := R.B;end; class operator TByte2. := (AB: Byte): TByte2;begin  Result.B := AB;end; var  B1: TByte1;  B2: TByte2;  B: Byte;begin  B1 := 123;  B := B1;  B2 := B;  B1 := Byte(B2); // OK  //B1 := B2; // Error: Incompatible types: got "TByte2" expected "TByte1"end. ---------------------------------------------------------------------- PascalDragon · 2022-02-06 11:00 Quote from: gbl on February 06, 2022, 08:07:51 amhow do I implement a strict type checking for fundamental types? Two types based on the same type will always be assignment compatible. There are some slight differences when you use a type alias as Thaddy suggested (e.g. for function overloads and type information), but that's it. Essentially you'd need to something like ASerge showed though it can be simplified a bit using generics. ---------------------------------------------------------------------- ASerge · 2022-02-06 12:55 Quote from: PascalDragon on February 06, 2022, 12:00:41 pmEssentially you'd need to something like ASerge showed though it can be simplified a bit using generics. Generics are not very suitable, because, "types are assignment compatible if the same template types are used". ---------------------------------------------------------------------- Warfley · 2022-02-07 00:43 The example of ASerge can also be extended to allow explicit casts between the two, but not implicit: 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 operator Explicit(const RHS: TByte1): TByte2; inline;begin  Result.B := RHS.B;end; operator Explicit(const RHS: TByte2): TByte1; inline;begin  Result.B := RHS.B;end; var  B1: TByte1;  B2: TByte2;begin  B2 := 42;  B1 := TByte1(B2); // OK  B2 := TByte2(B1); // OK  //B1 := B2; // Error: Incompatible types: got "TByte2" expected "TByte1" ---------------------------------------------------------------------- PascalDragon · 2022-02-07 12:38 Quote from: ASerge on February 06, 2022, 01:55:09 pmQuote from: PascalDragon on February 06, 2022, 12:00:41 pmEssentially you'd need to something like ASerge showed though it can be simplified a bit using generics. Generics are not very suitable, because, "types are assignment compatible if the same template types are used". Right...