Command–query separation principle [re: @mustuse as a function attribute]

Paul Backus snarwin at gmail.com
Wed Oct 19 18:56:35 UTC 2022


On Wednesday, 19 October 2022 at 18:38:55 UTC, mw wrote:
> That's because now we want to add @mustuse half-way, and it 
> requires global system analysis.

It does not require global system analysis.

> If D started from scratch as Eiffel did, and enforces all query 
> methods are @mustuse from the very beginning, such problem and 
> remedy headache would never existed.

Sure. It's the same with other attributes, like @safe and nothrow.

> So now we need to balance either we want prevent accidentally 
> (but fatal in most cases) discarding function returns, or we 
> want prevent more coupling between modules.

It is possible to achieve both. If the compiler enforces the 
rules I laid out in my initial post [1] (i.e., inheritance can 
only remove @mustuse, not add it), then accidentally discarding 
the return value of a @mustuse function will be impossible, *and* 
there will be no additional coupling between modules.

Of course, this means that there will be some functions that can 
never be marked as @mustuse without a breaking change--just like 
with @safe and nothrow. But that's true even with your proposal. 
Global analysis does not prevent the addition of @mustuse from 
breaking code, it just increases the number of places where that 
breaking change can be made.

[1] 
https://forum.dlang.org/post/cqlwlnpcbtbkzqnhicwc@forum.dlang.org



More information about the Digitalmars-d mailing list