Le 21/02/2012 11:55, deadalnix a écrit :
First great post. Don't be sorry, it is insightful. The Condition
catgory make a lot of sense to me.

Le 21/02/2012 09:15, H. S. Teoh a écrit :
Currently, I'm still unsure whether Conditions and Exceptions should be
unified, or they should be kept separate; deadalnix recommended they be
kept separate, but I'd like to open it for discussion.


I think we could reconciliate the 2 worlds. Condition may provide
@property Exception exception() so you can get the Exception.
Eventually, the Condition can return itself so it is the Exception.

This Exception can be used by the handler to get information about the
problem more specifically (if required). The Exception can also be used
to throw it, if no handler is provided, or if the handler decide it is
the right thing to do.

I suggested this mecanism should be implemented into std.condition and
still think this is the way to go. Addition to the language itself
should be as limited as possible (ie only occur if the compiler can take
advantage of this, for example to optimize, or if this isn't
implementable using the current state of the language).

This mecanism is not intended to replace Exception, but to provide a
more convenient, higher level (and probably more performant) than the
Exception mecanism. We fail back to Exceptions if this mecanism fail.

I come up with a sample code with the transient case. What do you think ?
module condition;

template isCondition(Condition) {
	enum bool isCondition = true;
}

// TODO: refactor this using something from std.container.
private template HandlerStack(Condition) if(isCondition!Condition) {
	void delegate(ref Condition)[] handlers = [];
	
	// static this to avoid : Error: non-constant nested delegate literal expression __dgliteral1
	// Not sure what does it mean and what to do with it.
	static this() {
		handlers ~= (ref Condition c) {
			throw c.exception;
		};
	}
	
	size_t bottom = 0;
	
	alias typeof(handlers[0]) DelegateType;
	
	struct HandlerRegistration {
		@disable this();
		@disable this(this);
		
		this(DelegateType dg) {
			bottom++;
			
			if(handlers.length > bottom) {
				handlers[bottom] = dg;
			} else {
				handlers ~= dg;
			}
		}
		
		~this() {
			handlers[bottom] = handlers[0];
			bottom--;
		}
	}
	
	DelegateType getHandler() {
		return handlers[bottom];
	}
}

auto registerHandler(Condition)(void delegate(ref Condition) dg) if(isCondition!Condition) {
	return HandlerStack!Condition.HandlerRegistration(dg);
}

void raise(Condition)(ref Condition c) if(isCondition!Condition) {
	HandlerStack!Condition.getHandler()(c);
}

// Transient
struct TransientCondition {
	private bool _retry;
	
	// TODO: We should be able to pass a delegate to generate the Exception.
	@property
	Exception exception() {
		return new Exception("transient");
	}
	
	void retry() {
		_retry = true;
	}
}

auto runTransient(alias operation, alias successTest = (bool res) => res)() if(is(typeof(successTest(operation())) == bool)) {
	void fail() {
		auto c = TransientCondition();
		raise(c);
		
		if(!c._retry) throw c.exception;
	}
	
	while(true) {
		try {
			auto res = operation();
			
			if(successTest(res)) {
				return res;
			} else {
				fail();
			}
		} catch(Exception e) {
			fail();
		}
	}
}

import condition;

void main() {
	try {
		int nbRetry = 5;
		auto handlerRegistration = registerHandler((ref TransientCondition c){
			if(nbRetry > 0) {
				import std.stdio;
				writeln("Transient Condition ! Retry !");
				
				c.retry();
			}
			
			nbRetry--;
		});
		
		runTransient!({
			return false;
		})();
	} catch(Exception e) {
		import std.stdio;
		writeln("Exception thrown !");
	}
}

Reply via email to