You're Doing In-Conditions Wrong

FeepingCreature feepingcreature at gmail.com
Tue Jul 14 12:05:23 UTC 2020


On Tuesday, 14 July 2020 at 10:19:51 UTC, burt wrote:
> On Monday, 13 July 2020 at 13:55:56 UTC, FeepingCreature wrote:
>> [...]
>>
>> Let's consider two cases: debug modes, where we want to look 
>> for and find logic errors, and non-debug mode, where we just 
>> want to run correctly.
>>
>> Within debug mode, D should enforce that in contracts loosen 
>> the conditions. As such, it should always execute both 
>> superclass and subclass contract and Error if superclass-in 
>> passes but subclass-in does not.
>
> I don't believe this is actually the case; it should not throw 
> an Error if superclass-in passes and subclass-in does not. 
> Consider the following case:
>
> [snip]

I think the disagreement here is whether an incondition should 
mean "a condition for the method" or "a condition that is added 
to the implicit disjunction of the parent inconditions."

I think the way that D works currently is bad. I'm raising a 
design criticism here, not a bug - I know the current behavior is 
per spec. But I mean, if you see

```
class B : A {
     void method(int x) in (x == 3) {}
}
```

You don't expect x to be 2. In fact, the vastly more plausible 
way to arrive at this code is that the parent used to check `x == 
3` but was changed to check `x == 2`. The disjunctive approach 
gives up the chance to discover this bug, for no benefit. Why no 
benefit? To be frank, because this kind of example essentially 
never comes up in practice.

Something like 95% of inconditions in our codebase at least, are 
some variant of "not null". How do you relax this incondition? By 
not writing anything, in both proposals. You certainly don't 
write `in (obj is null)`.

When do you want to add an additional disjunctive check that is 
totally unrelated to the parent's check? Even if you're say, 
expanding an enum, the expanded check will simply be "is the 
value in the expanded enum," not "is the value one of the two new 
enum values that I added."

I think "restate the parent's condition plus your new values" is 
already what people do anyways. Might as well take advantage.


More information about the Digitalmars-d mailing list