[fpc-devel] Question about "Default()"

Martin Frb lazarus at mfriebe.de
Thu Feb 22 14:27:01 CET 2024


https://www.freepascal.org/docs-html/rtl/system/default.html
> Default is a compiler intrinsic: it returns for every type T a default 
> value. In essence, this is a block of memory that is zeroed out. It 
> can be used to correctly initialize any type, and more importantly, a 
> managed type. It also works using a generic type template. 

But zero isn't always a valid value => so how can it be used to 
initialize a type where that is invalid.

The below will runtime error "invalid enum value".
(And also, in the past, I saw it pointed out countless times, that 
setting an enum to an ordinal value that is not matching any of its 
members does not have a defined behaviour).

So is that a bug in Default?
Or is the documentation wrong "any type"? (it contradicts itself anyway 
"zeroed" <> "any type")
Or ...?

program Project1;
type
   //tt = 3..5;
   tt = (e1=3, e2=5,e3=7);
var a: tt;
begin
   a := Default(tt);
   writeln(a);
   readln;
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20240222/db5a4071/attachment.htm>


More information about the fpc-devel mailing list