Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-10

default value of a sub range field produces No range Check error

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dennis
I tried with fpc 2.6.4
-----------------
program Project1;
uses classes;
type
   TSmall = 4..12;
TA=class
public
   N : TSmall; //default to 0 after create, should raise range check error
end;
var
    A : TA;
begin
   A := TA.Create; //no range check error
   writeln(A.N); //no range check error
   inc(A.N);  //range check error
   writeln(A.N);
   readln;
end.

--------------

the fact that it raises NO range check error during Create and the first writeln seems weird.

Perhaps the compiler should have a warning when defining a sub-range field in class, telling users that it will be default to 0.

Dennis

leledumbo
to Dennis
the fact that it raises NO range check error during Create and the first writeln seems weird.

Line 16 (Inc statement) should raise one (ensure you're compiling with the correct option, by default range checking is off), the others shouldn't. Range check occurs on write, not read.

Jonas Maebe
to Dennis: I tried with fpc 2.6.4 ----------------- program Project1; uses classes; type TSmall =…
Dennis wrote
the fact that it raises NO range check error during Create and the first writeln seems weird.

Range checks are inserted when a conversion from type A to type B occurs, with A B (and then it checks whether the value is a valid "type B" value). Values of fields in constructors are not explicitly assigned, the instance is simply backed by a memory area that has been set to zero.

Perhaps the compiler should have a warning when defining a sub-range field in class, telling users that it will be default to 0.

The documentation says this already:
http://www.freepascal.org/docs-html/3.0.0/ref/refsu24.html ("The example demonstrates that values of fields are initialized with zero (or the equivalent of zero for non ordinal types: empty string, empty array and so on).") A warning would be overkill, I think.

Jonas