www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - BRAINSTORM: Exceptions using delegates, goto: out of a delegate literal?

reply Russell Lewis <webmaster villagersonline.com> writes:
Imagine that you could goto: out of a delegate literal into the original 
function.  This would allow you to perform a language-friendly, compiler 
directed longjmp().  Moreover, since it uses the existing delegate 
literal mechanisms, it should be a lot faster than setjmp/longjmp.  You 
don't have the runtime cost of a setjmp (no need to do any snapshotting 
other than what the function would do anyway), and the goto itself would 
(presumably) be much less costly than a longjmp, since the compiler 
knows exactly what registers to restore (presumably few, since the 
function's locals would reside on the stack or heap already).

Now imagine that you used this facility to build an elegant alternative 
to exceptions: exception-safe functions would have an implicit argument 
which was the exception handler delegate.  To define a new exception 
handler, you write a new delegate and pass it as the current handler to 
any functions you call.  (Your new delegate can chain to the old if you 
desire to implement catch-and-throw or catching of only certain types.)

Ofc, this means that each exception-safe function has the added cost of 
an implicit delegate parameter, which I don't want to ignore, but I will 
neglect it for a bit.

The big downside of the above idea (other than the runtime cost I'm 
neglecting) is that the syntax is nontrivial.  A exception handler would 
be something like this:

	void func() {
	  SomeOtherFunc(delegate void(Exception e) { <handler code>
	                                             goto _out; },
	                <some other, normal parameters);
	_out:
	  return;
	}

Is there some way that we could leverage or alter the exception syntax 
in D to give us some syntax sugar?  This is very akin to how foreach 
works: the compiler adds magic to make a common paradigm easy to code 
and easy to read.  What about something like:

	void func() {
	  keyword1 {
	    SomeOtherFunc(<normal params>);
	  }
	  keyword2(Exception e) {
	    <handler code>
	    goto _out;
	  }

	_out:
	  return;
	}

The cool thing is that if we get this syntax right, then it becomes a 
general-purpose language feature which would provide exception handling 
as just one special case.

Thoughts?
Jan 20 2008
next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On Jan 21, 2008 5:15 AM, Russell Lewis <webmaster villagersonline.com> wrote:
 Imagine that you could goto: out of a delegate literal into the original
 function.

We still haven't deprecated goto yet? I'd forgotten that existed.
 Thoughts?

We already have such a thing. It's called throw.
Jan 21 2008
prev sibling next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
Russell Lewis wrote:
     void func() {
       keyword1 {
         SomeOtherFunc(<normal params>);
       }
       keyword2(Exception e) {
         <handler code>
         goto _out;
       }
 
     _out:
       return;
     }

Replace keyword1 with "try" and keyword2 with "catch". Problem solved!
Jan 21 2008
prev sibling parent reply "Craig Black" <cblack ara.com> writes:
Interesting idea, but you failed to highlight the other things that could be 
done with this besides exceptions.  Further, the syntax for exceptions is 
pretty well established, so any modifications to it would require very 
compelling explanations.  It's easy to bash someone with a new idea, so 
sorry if it seems like I'm bashing.

-Craig 
Jan 21 2008
parent Russell Lewis <webmaster villagersonline.com> writes:
Craig Black wrote:
 Interesting idea, but you failed to highlight the other things that could be 
 done with this besides exceptions.  Further, the syntax for exceptions is 
 pretty well established, so any modifications to it would require very 
 compelling explanations.  It's easy to bash someone with a new idea, so 
 sorry if it seems like I'm bashing.

Actually, you're the most charitable of the responses. :) I agree that I have not stated any compelling "other use" for this. It's because I don't know of one, yet. But my gut says that this falls in the category of "has many unexpected uses" features. I thought I would throw it out for others to chew on before I forgot it. Let's set aside the syntax sugar thing I proposed for a moment and just look at the goto: proposal. IMHO, having a language-supported longjmp is certainly an intriguing idea. The only thing I can think of right now that would make it hard would be that you'd need to clean up finally blocks and auto variables as you accomplished the goto. Hmm.
Jan 22 2008