Const transitivity is bad sometimes

naryl cy at ngs.ru
Wed Jan 16 15:55:27 PST 2008


On Wed, 16 Jan 2008 21:45:43 +0300, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> "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
>
>

Thanks! I forgot that message is intended to be invariant-only. Then I can  
build it in the class and leave notifier mutable.


More information about the Digitalmars-d-learn mailing list