Constant relationships between non-constant objects

Rikki Cattermole via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 17 23:15:48 PDT 2014


On 18/06/2014 6:05 p.m., Maxim Fomin wrote:
> On Wednesday, 18 June 2014 at 05:34:18 UTC, Sebastian Unger wrote:
>>
>> So again, I believe, if D wants to play any role in major OO software
>> design and development, it will need to step up its game. Especially
>> in view of C++11 having addressed a number of the issues in C++ that I
>> would have chosen D over C++ for.
>>
>> Cheers,
>> Seb
>
> I believe this is wrong. First of all, if you want to keep relationship
> between objects, instead of thinking which feature can prevent it,
> consider not to try to break it in a first place.
>
> Secondly, it is sometimes discussed how to write some idiomatic code per
> se, rather than to solve particular task.
>
> Thirdly, if feature from one language does not do the same as similar
> feature from other language, does not mean that the feature/language is
> broken.

+1

> Lastly, taking into account that it was Walter decided to do so, says
> something.
>
> Regarding your problem. Keeping relationship between two classes can be
> achieved by associative array, by inheriting from one class or by using
> examples above.

There is also always things like property getter functions if you really 
really want to be pedantic about it.

class A {
     this(B b) {
         b_ = b;
     }

     private B b_;

     @property B b() {
         return b_;
     }
}

class B {
	string text;	
	
	this(string text) {
		this.text = text;	
	}
}

void main() {
	import std.stdio;
	A a = new A(new B("hi there"));
	writeln(a.b.text);
	a.b.text = "boo";
	writeln(a.b.text);
}

In this case, literally no way to get access to b_ variable directly 
unless you are in the same module.

To prevent assignment within the same module, a wrapper would be needed 
as has been discussed.

Personally I wouldn't use code like this. But thats just me.


More information about the Digitalmars-d mailing list