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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

leledumbo
Administrator
> 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.
Reply | Threaded
Open this post in threaded view
|

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

Jonas Maebe-2
In reply to this post by Dennis
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

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

Peter
In reply to this post by Dennis
On 15/10/15 16:58, Dennis wrote:
>
> Perhaps the compiler should have a warning when defining a sub-range
> field in class, telling users that it will be default to 0.
>

Hi,

The problem is not restricted to classes.


Type
  TSmall = 4..12;

Var B : TSmall;
...
    Writeln (B):


Shows the same issue. Takes default value of zero; no range check on the
first writeln.


Regards,
Peter
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal