[fpc-pascal] Optional param modifier

Martin Frb lazarus at mfriebe.de
Sat Apr 13 18:51:50 CEST 2019


On 13/04/2019 18:40, Benito van der Zander wrote:
> Hi,
>
> the parameter is already optional without any modifier, since you can 
> always pass nil for it.
>
> Perhaps there could be the opposite modifier, so the function cannot 
> be called with nil. Like
> procedure DoThis(nonnil var obj: TObject);
>
> and then you need to check if it is nil, before calling DoThis .
>
>
> Or you put it in the type. type TSomeObject = nonnil class(TObject)... 
> and then it would never be allowed to be nil.
> Or for pointers type PDude = nonnil ^TDude;
> If you would want to use such a nonnil class as field in another 
> class, you would need to initialize it in the constructor.
>

Well nil is only with objects. For strings you could expect that there 
are not empty.
For integers you can have sub-ranges. But for float, you may have a 
condition that says > 0.

I think this goes towards https://en.wikipedia.org/wiki/Design_by_contract
pre/post conditions.

Those can then be applied for anything. And the compiler can add 
compile/runtime checks, the same way it does range checking.

As stated on wikipaedia this also plays towards docs. If a function has 
a postcondition that either forbids nil, or explicitly allows it, then 
you do not need to look at its code. You know what to expect.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20190413/cdcc3313/attachment.html>


More information about the fpc-pascal mailing list