Recent

Author Topic: Turn off implicit type casting  (Read 2316 times)

Okoba

  • Hero Member
  • *****
  • Posts: 528
Turn off implicit type casting
« on: July 31, 2021, 11:08:11 am »
Hi,

Is there a way to set the compiler implicit type casting off?
I want the compiler to stop compiling the assignment as they are two different types.
I know they are technically integers, but I like to use them separately and be safe so that the compiler won't let me assign them by mistake.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   Type1 = type Integer;
  5.   Type2 = type Integer;
  6.  
  7. var
  8.   T1: Type1;
  9.   T2: Type2;
  10. begin
  11.   T1 := T2;
  12. end.          

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Turn off implicit type casting
« Reply #1 on: July 31, 2021, 11:47:50 am »
I don't believe so, but wish there were: either for a specified block of code or as a trait of a type when it was defined.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Turn off implicit type casting
« Reply #2 on: July 31, 2021, 11:52:07 am »
I hope too, it can be very handy. Hope some of the dev team can have an answer for this.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
Re: Turn off implicit type casting
« Reply #3 on: July 31, 2021, 12:08:38 pm »
If you want that, wrap them in different record types. I understand this won't allow you to use them directly in calculations, but that would not be possible either if the compiler refused implicit type casting for such types. The reason is that in Pascal, every type is implicitly typecasted to the native integer type when performing calculations.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Turn off implicit type casting
« Reply #4 on: July 31, 2021, 01:37:39 pm »
Thanks for that Jonas. So if I understand you correctly assignment of (or reference to) an entire record will never be cast, although the contained field(s) will be.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: Turn off implicit type casting
« Reply #5 on: July 31, 2021, 02:07:41 pm »
@Jonas Is it a always thing or there can be a option to turn it off? For example when you assign an Int64 to a byte, it warns you, but not other way around, and I wish there were a way I could find that out.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
Re: Turn off implicit type casting
« Reply #6 on: July 31, 2021, 02:18:11 pm »
No, we don't have an option to warn for operations that are guaranteed to be safe in all situations, and I'm not aware of anyone planning on adding support for that. It's Pascal, not Ada :)

 

TinyPortal © 2005-2018