On Wed, May 16, 2012 at 10:54:26PM +0200, Mehrdad wrote:
[...]
> Haha maybe, idk. I just wrote what I wrote so that I could use it
> like:
> 
> noThrow({
>       // giant block of code
> });
> 
> 
> to execute it as nothrow.

Whoa, this code works:

        import std.math;
        import std.stdio;

        T funcWrap(T,U...)(scope T function(U) f, U args) {
                writeln("Calling wrapped function");
                scope(exit) writeln("Wrapped function returned");

                return f(args);
        }

        void printInt(int x) {
                writeln(x);
        }

        float computeFloat(float x, float y) {
                return x^^2 + y;
        }

        void main() {
                funcWrap(&printInt, 12345);
                writeln("Result is: ", funcWrap(&computeFloat, 3.0f, 1.5f));
        
                funcWrap({
                        writeln("Inside an anonymous delegate");
                });

                funcWrap((int x) {
                        writeln("Delegate with parameter: ", x);
                }, 100);
        }

Output:

        Calling wrapped function
        12345
        Wrapped function returned
        Calling wrapped function
        Wrapped function returned
        Result is: 10.5
        Calling wrapped function
        Inside an anonymous delegate
        Wrapped function returned
        Calling wrapped function
        Delegate with parameter: 100
        Wrapped function returned

OK, this isn't the same as your nothrow wrapper, but the principle is
the same. The funcWrap template can basically call _any_ function that
returns _anything_.

D just acquired whole new levels of cool for me. :-)


T

-- 
ASCII stupid question, getty stupid ANSI.

Reply via email to