Constant relationships between non-constant objects

Meta via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 17 21:38:23 PDT 2014


On Wednesday, 18 June 2014 at 01:31:33 UTC, Sebastian Unger wrote:
> Hi there,
>
> I'm an experient C++ developer and am trying to switch to / 
> learn
> D. What I've seen so far is mostly quite straight forward and
> VERY nice.
>
> There's only one catch so far for me for which Googling has only
> found the discouraging answer of: It can't be done in D.
>
> I have two classes A and B. Each object of class A is associated
> with a particular object of class B. This association is not
> supposed to change throughout the lifetime of the object of A.
>
> How am I supposed to express this in D, given that D's const is
> too strong for this? I don't need any guarantees from the const
> that can be used for thread safety or parallelisation. All I 
> need
> is the compiler not letting me change the reference to the B
> object inside the A object.
>
> Does D have some way of expressing this?
>
> Or has D really done away with the MOST important use case of
> const (preventing developer mistakes! Not optimization.)
>
> Cheers,
> Seb

There's no head const in D, but you can emulate it. This is a 
*very* basic example that you can expand upon.

http://dpaste.dzfl.pl/55cb22153fcc


More information about the Digitalmars-d mailing list