Der schöne Günther 323 Posted Wednesday at 12:15 PM (edited) Consider the following code: program Project1; uses System.SysUtils; type TSubRange = 1..10; var subRange: TSubRange; begin subRange := Default(TSubRange); Assert(subRange = Low(TSubRange)); // subRange should be 1, but is 0 end. I was under the impression, that the default value of TSomeRange would be 1 and not 0, a value that can neither be set at compile time, nor at runtime. Documentation of System.Default - RAD Studio API Documentation is non-existent. Documentation about subrange types (Simple Types (Delphi) - RAD Studio) is a bit more helpful: Quote The ordinality of each value in a subrange is preserved from the base type. (...) Values do not wrap around the beginning or end of a subrange, even if the base is an integer or character type; incrementing or decrementing past the boundary of a subrange simply converts the value to the base type. (...) Hence, while: I := 100 produces an error, I := 99; Inc(I); assigns the value 100 to I (unless compiler range-checking is enabled). In my case, it doesn't matter whether range checking at runtime is enabled or disabled. Default(TSubRange) seems to gloss over the definition of TSubRange, thinks "Hey, it's a byte" and just outputs 0. My question: - Is this valid behaviour? Has it changed? I am using Delphi 11.1 - Are there possibly any other caveats with subrange types? Maybe just not use them at all? Edited Wednesday at 12:17 PM by Der schöne Günther Share this post Link to post
Lajos Juhász 311 Posted Wednesday at 12:22 PM It is not (yet) addressed in Delphi 12.2 it still defaults to 0 instead of 1. 1 Share this post Link to post
Anders Melander 1894 Posted Wednesday at 12:56 PM 26 minutes ago, Lajos Juhász said: It is not (yet) addressed in Delphi 12.2 it still defaults to 0 instead of 1. Don't expect it to be "fixed". The best you can hope for is that it is documented. The current behavior is consistent with class initialization; If you have the same field in a class and create a new instance, then the ordinal value of the field will be zero - as expected. Share this post Link to post
DelphiUdIT 209 Posted Wednesday at 01:15 PM For each data whose value I need to know for sure before using it, I perform an assignment operation of a certain value at the creation of any "parent" or at the creation of the application. I had some problems with the "default" in the past, so I equipped myself and I do it like this. I know it's probably excessive, but so far I haven't had any issues. Share this post Link to post