[fpc-pascal] Optional param modifier

Sven Barth pascaldragon at googlemail.com
Mon Apr 15 09:35:06 CEST 2019


Am 15.04.2019 um 01:19 schrieb Ryan Joseph:
>
>> On Apr 14, 2019, at 7:08 PM, Martin Frb <lazarus at mfriebe.de> wrote:
>>
>> Almost. assert takes the condition itself
>>
>> assert(condition_that_must_be_true, 'error message')
>>
>> so it would be
>>    assert(name<>'', 'requires "name <> ''''" failed for CreateHero');
>>
>> You can already insert such asserts yourself. assert exists.
>>
>> If you compile with -Sa then they are compiled in the code, otherwise not.
>  From the compilers perspective isn’t it faster to test the condition first in an if statement so the assert function doesn’t get called? Assert tests for false also so it needs to invert the test to “name = ‘’” which is not how the “requires” statement is worded.
An Assert() might look like a mere procedure call, but it's in fact an 
intrinsic. If assertions are disabled ({$ASSERTIONS OFF}) then it 
completely does not exist and otherwise it's the check followed by a 
call to 'fpc_assert'.

Also an assertion "fires" if it's condition is False, so Assert() would 
do the same check as "requires" or "ensure" need:

requires
   Assigned(MyArg)

becomes

Assert(Assigned(MyArg), ...);

Regards,
Sven



More information about the fpc-pascal mailing list