[fpc-pascal] Option type

Sven Barth pascaldragon at googlemail.com
Wed Jun 2 14:20:10 CEST 2021


denisgolovan via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mi., 2. Juni 2021, 13:28:

>
>
> > Well as already discovered type like strings can not go into a "record
> case"
> >
> > But... The above record is anyway of constant size. I.e. the memory for
> > the field is always included, even if it is not used.
> >
> > Since the "false" block is empty, you can do
> >
> > type
> > generic TOption<T> = record
> > IsSome:boolean;
> > some: T;
> > end;
> >
> > It is not as expressive to the reader of the code. But it leads to the
> > same data in memory.
>
> Yes.
> Except that "some" is still initialized with something (some default) and
> dropped and copied and assigned despite conceptually being empty.
>

That would have to be the case otherwise as well. And even worse: the RTL
would need to decide at runtime if it would need to manage that entry.
With the suggested solution (which is essentially what TNullable<> provides
as well) this will be rather cheap if the element is Nil (aka empty)
anyway.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210602/d59ca623/attachment.htm>


More information about the fpc-pascal mailing list