Const transitivity is bad sometimes

Steven Schveighoffer schveiguy at yahoo.com
Wed Jan 16 10:45:43 PST 2008


"naryl" wrote
> For example, I have this piece of (useless) code:
>
> http://paste.dprogramming.com/dpmmwnah
>
> If there were more subscribers the same message would be sent to every one 
> of them. And there may be several messages, carrying the same notifier as 
> their data. While compiling this I got the following errors (DMD 2.009):
>
> main.d(28): function main.SystemShutdownNotifier.addSubscriber 
> (Subscriber) does not match parameter types (Subscriber)
> main.d(28): Error: (cmd.notifier).addSubscriber can only be called on a 
> mutable object, not const(SystemShutdownNotifier)
>
> I know. Notifier has become const because const is transitive, but in this 
> example I don't need transitivity. I can't even think of a workaround for 
> that. Maybe someone can help?

This is an example of something that should be re-designed.  It looks more 
like the subscriber should contain the notifier, not the message.  However, 
I don't see where this stuff is used, so I'm not sure how you designed the 
rest of it.

Another option is to build in the invariance in the class itself.  So 
instead of making the whole message invariant, just make the portions that 
aren't going to change invariant (your example really doesn't have any other 
members, but I'm assuming normal messages have other data that you want to 
be invariant?), and then you are free to modify the notifier.

-Steve 




More information about the Digitalmars-d-learn mailing list