www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D1 & D2 discrepancy

reply "lanael" <lanael free.fr> writes:
The following piece of code compiles without error on D1(1.072) & 
D2(2.057).
However the D2 version doesn't run fine. ( output below )

---------------------------->8----------------------------
abstract class Receiver(M) {
	this() { Broadcaster!(M).register(this); }
	~this() { Broadcaster!(M).unregister(this); }
	abstract void onMessage( M message );
}

abstract class Emitter(M) {
	void emit(M message) { Broadcaster!(M).broadcast(message); }	
}

static class Broadcaster(M) {
	private static Receiver!(M)[string] mReceivers;

	public static void register( Receiver!(M) receiver ) {
		mReceivers[receiver.toString()] = receiver;
	}

	public static void unregister( Receiver!(M) receiver ) {
		mReceivers.remove(receiver.toString());

	}

	public static void broadcast( M message ) {
		foreach( ref receiver; mReceivers ) {
			receiver.onMessage( message );
		}
		delete message;
	}
}
//================================================== import 
std.stdio;
void main() {
	class TestMessage {
		this( string m ) { stringAttribute = m; }
		string stringAttribute;
	}

	class TestReceiver1: Receiver!(TestMessage) {
		this() { super(); }
		override void onMessage( TestMessage m ) {
			writefln( "receiver 1 :", m.stringAttribute );
		}
	}


	class TestReceiver2: Receiver!(TestMessage) {
		this() { super(); }
		override void onMessage( TestMessage m ) {
			writefln( "receiver 2 :", m.stringAttribute );
		}
	}


	class TestEmitter: Emitter!(TestMessage) {
		void send() {
			emit( new TestMessage("Hey") );
		}
	}

	auto e = new TestEmitter;
	auto r1 = new TestReceiver1;
	auto r2 = new TestReceiver2;

	e.send;
	delete r2;
	e.send;
}
----------------------------8<----------------------------

* Output of the D1 version :
receiver 1 :Hey
receiver 2 :Hey
receiver 1 :Hey


* Output of the D2 version :
receiver 1 :
receiver 2 :
receiver 1 :


Is this a bug or something stupid on my part ?
Dec 23 2011
next sibling parent reply Trass3r <un known.com> writes:
 * Output of the D1 version :
 receiver 1 :Hey
 receiver 2 :Hey
 receiver 1 :Hey


 * Output of the D2 version :
 receiver 1 :
 receiver 2 :
 receiver 1 :


 Is this a bug or something stupid on my part ?

Looks suspicious. Debug it to see what gets called.
Dec 23 2011
parent =?UTF-8?B?xYF1a2FzeiBXcnpvc2Vr?= <lukasz gmail.com> writes:
W dniu 23.12.2011 14:37, Trass3r pisze:
 * Output of the D1 version :
 receiver 1 :Hey
 receiver 2 :Hey
 receiver 1 :Hey


 * Output of the D2 version :
 receiver 1 :
 receiver 2 :
 receiver 1 :


 Is this a bug or something stupid on my part ?

Looks suspicious. Debug it to see what gets called.

writefln should be replaced with writeln and it works on d2 too.
Dec 23 2011
prev sibling parent Jesse Phillips <jessekphillips+d gmail.com> writes:
On Sat, 24 Dec 2011 01:45:56 +0100, Ɓukasz Wrzosek wrote:

 writefln should be replaced with writeln and it works on d2 too.

D1 does not have a writeln function: http://digitalmars.com/d/1.0/phobos/std_stdio.html
Jan 02 2012