D doesn't have real closures

Guillaume B. guillaume.b.spam at spam.spam
Thu Sep 13 14:59:29 PDT 2007


Julio César Carrascal Urquijo Wrote:

> Brad Anderson wrote:
> > http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/
> 
> I've posted a response on my blog:
> 
> http://jcesar.totumo.net/2007/09/13/d-should-have-real-closures/
> 
> Comments?
> 
> Thanks

I'm pretty much new to D but I've recently found std.bind.bind() and while it's not as nice, it seems to work:

==========
import std.stdio;
import std.bind;

class Event {
	private void delegate()[] m_Delegate;
	
	public void opCatAssign(void delegate() dg) {
		m_Delegate ~= dg;
	}

	public void fireEvent() {
		foreach (dg; m_Delegate) {
			dg();
		}
	}
}

void bindEvent(T)(ref Event event, ref T source, ref T target) {
	void f(ref T source, ref T target) {
		writefln("target: %s = source: %s", target, source);
	}
	event ~= bind(&f, source, target).ptr;
}

void bindSomeEvent(Event event, char[] s) {
	auto source = "source" ~ s;
	auto target = s ~ "target";
	bindEvent(event, source, target);
}

void main() {
	auto e = new Event();
	bindSomeEvent(e, "1");
	bindSomeEvent(e, "2");
	bindSomeEvent(e, "3");
	e.fireEvent();
}
==========

Is there something wrong with this? ... Well, real closures would be much nicer, but I want to know if I'm missing something...

 Thanks,

 Guillaume




More information about the Digitalmars-d-announce mailing list