[fpc-pascal] Optional param modifier
ryan at thealchemistguild.com
Mon Apr 15 00:53:31 CEST 2019
> On Apr 14, 2019, at 6:30 PM, Martin Frb <lazarus at mfriebe.de> wrote:
> I would actually argue that it is infinitely more important to check for nil after MakeThing. (Unless I know for sure it is not needed: proof, docs)
> Assuming (taking the danger of doing so) both do what there name indicates, then:
> - If I forget to check after FindThing, it is likely to cause an error very soon, and most probably while I am testing myself.
> - If I forget it after MakeThing, it may be very rare to cause an error. It will likely pass my tests, and cause a customer of mine some stress.
I understand all your concerns over not wanting to use this so I would suggest to avoid it (we all feel that way about certain features). Since no-one thinks this is a good idea I’ll forget about because it’s not that important anyways. Waiting for nullable types is maybe a better option anyways and those are in the pipeline via default properties.
What Sven brought up is more immediately useful anyways and I can think of many times I have the mandatory top part of the function that checks to make sure parameters are valid. Formalizing this process would be a smart thing I think. If you don’t agree, then don’t use it. ;)
That’s what I read from the link posted. As for the “ensure” block now that I don’t see as a pattern that I can recognize. Any real world examples of this?
Here’s what I imagine that would look like:
procedure CreateHero (name: string; hp: integer);
name <> '';
(hp > 0) and (hp < 100);
// compilers parses the conditions and inserts as if statements:
// if name <> ‘’ then
// assert(‘condition "name <> ‘'" for CreateHero failed’);
// if (hp > 0) and (hp < 100) then
// assert(‘condition "(hp > 0) and (hp < 100)" for CreateHero failed);
Pretty simple right?
More information about the fpc-pascal