Shared an non-shared

Begah via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 4 12:22:10 PDT 2016


I seem to be missing something.
It seems that if you want to create a shared object of a 
structure ( or class ), then I have to copy every functions and 
add "shared" to it. This seems way more work than it should.
For example why can't this simply work :

   class Image {
	string name;
	int contents;
	
	this(string name, int contents) {
		this.name = name;
		this.contents = contents;
	}
	
	void printContents() {
		writeln(name, " : ", contents);
	}
	
	void changeContents(int newContents) {
		this.contents = newContents;
	}
   }

   void main()
   {
	Image im = new Image("Test", 15);
	im.printContents();
	im.changeContents(45);
	im.printContents();
	
	shared Image imShared = new shared Image("Test2", 80);
	imShared.printContents();
	imShared.changeContents(6);
	imShared.printContents();
   }

How can I make a method that accepts being called by both a 
shared and non-shared object, to prevent having to copy methods 
and adding a "shared"?

I am not looking for an explanation for how to handle 
multi-threading ( synchronization and so on ). I am looking to 
use pre-coded classes and structures ( without using __gshared ) 
with non-shared and shared objects.


More information about the Digitalmars-d-learn mailing list