BRAINSTORM: return type of 'exception'

Russell Lewis webmaster at villagersonline.com
Tue Jan 13 09:06:49 PST 2009


So I was listening to the discussion about opApply in the "lazy" thread. 
  Somebody mentioned that opApply would be better if we could eliminate 
the int return code.

So I thought, "well, you could make the unusual situations (return, 
break, etc.) exceptions, but that would be too slow..."

Then I thought, "wouldn't it be cool if the compiler could recognize 
that we had a void return value, and somehow convert the exception into 
a simple return code..."

Then I thought, "what if we could delcare a function's return type to be 
'exception'?"

The idea here is that if you had return type 'exception', then 
exceptions coming out of that function would be returned as return 
values rather than thrown with the ordinary mechanism.  The calling 
function would be responsible for checking that return code and handling 
it properly if an exception were returned.

So, inside this type of function, "throw" statements would be converted 
(by the compiler) into "return"s.  And when you called a function of 
this type, it would check the return code and throw the proper exception 
if one was returned.  In something like opApply, where you have this 
type of function calling this type of delegate, the compiler-generated 
code would look like opApply does now.

PROPOSED SYNTAX
	exception opApply(exception delegate(ref MyType) dg)
	{
		<do some looping stuff>
			dg(<thing>)
			// compiler checks the return code for us!
	}
END SYNTAX

Thoughts?



More information about the Digitalmars-d mailing list