[fpc-pascal] Optional param modifier

Martin Frb lazarus at mfriebe.de
Mon Apr 15 01:30:01 CEST 2019


On 15/04/2019 01:19, Ryan Joseph wrote:
>
>> 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.
>
> I’ve always made a “fatal” function which works in the opposite direction as assert, i.e if the condition is *true* then crash. Don’t know why but that’s easier for me to understand. I like the requires section because it works in the same logic.
>
You had

requires
   name <> '';

Or in other words: bail if name = ''

   assert(name<>'', ...)
does that.
It bails if name = ''. (then the condition "name <> '' " would be false 
=> bail)

requires, like assert want the condition(s) to be true

Assert is for testing, so speed doesn't matter.
I don't know how it is internally implemented.




More information about the fpc-pascal mailing list