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

mw mingwu at gmail.com
Wed Oct 19 17:40:00 UTC 2022


Right now in this discussion thread there are two aspects:
1) the language logic, and
2) the implementation logic (and current status, including 
pre-built binaries).

I'm not interested in 2). We need to first make the language 
logic correct.

> If indeed @mustuse's purpose is to prevent accidentally 
> discarding
> important return values, i.e., something marked @mustuse must 
> *never* be
> silently dropped, then neither (1) nor (2) is acceptable.

That's because you still consider the @mustuse attribute on each 
class level individually, if you think about the global system 
analysis as a whole, it will work, and consistently. Yes, what I 
proposed is transitive closure:

     /--------Base------\
     |        |         |
Derived1   Derive2  Derive3

If the programmer only manually marked Derive3.remove() as 
@mustuse, everything else (Base, Derived1   Derive2  Derive3)'s 
.remove() method will become @mustuse (as seen by the compiler 
internally).


> This is not enforceable, since the base class and derived class 
> could be in two far-flung files, and due to incremental 
> compilation the compiler cannot enforce @mustuse in an unmarked 
> method in a class that might have some distant relative in the 
> inheritance tree that has @mustuse.

(all these comments are compiler implementation issues (2), which 
I don't want to comment)


>> That is why I say:
>> 
>> if a method is marked as @mustuse anywhere in the inheritance 
>> tree, that method in all the class levels in the whole 
>> inheritance tree became @mustuse!
>
> This is correct.  But it cannot be implicit; if you attribute a 
> method @mustuse somewhere in the inheritance tree, then you 
> must also write @mustuse in every other class in the hierarchy. 
> Otherwise it's not enforceable.

I'm glad you agree with my global analysis as a whole view here.

(I want the compiler to propagate and inject such attribute is 
only to save the programmers manual work, by no means it means 
it's implicit.

As to the compiler implementation, I'll let Walter figure it out).



More information about the Digitalmars-d mailing list