Another idiom I wish were gone from phobos/druntime
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Wed Feb 4 16:50:05 PST 2015
On Thursday, 5 February 2015 at 00:42:01 UTC, Andrei Alexandrescu
wrote:
> On 2/4/15 4:40 PM, Jonathan Marler wrote:
>> On Thursday, 5 February 2015 at 00:35:50 UTC, bearophile wrote:
>>> Contracts can be read by tools, and they are part of the
>>> function
>>> signature. Contracts should be encouraged and increased, not
>>> discouraged.
>>>
>>> Bye,
>>> bearophile
>>
>> Not to mention that contracts can be removed by the compiler
>> at compile
>> time.
>
> Same about asserts. -- Andrei
Only if the function is inlined. The function variables are
unknown at compile time so there's no way for the function to
compile it away (unless the optimizer can see every single call
to the function). If you make the caller responsible for the
assert then it can determine whether or not it needs to perform
it at runtime.
Then what about chaining contracts. If a function has an out
contract saying that the return value will be between a certain
range, and you use that same variable in another function that
requires it to be in the same range, you can compile away any
assert after the first one. This is trivial to do with contracts.
You could do it without contracts I suppose, but would be much
more brittle (every single return location would have to have the
same assert for example)
More information about the Digitalmars-d
mailing list