On 2009-09-22 12:32:25 -0400, Andrei Alexandrescu <seewebsiteforem...@erdani.org> said:

Daniel Keep wrote:
P.S. And another thing while I'm at it: why can't we declare void
variables?  This is another thing that really complicates generic code.

How would you use them?

Here's some generic code that would benefit from void as a variable type in the D/Objective-C bridge. Basically, it keeps the result of a function call, does some cleaning, and returns the result (with value conversions if needed). Unfortunately, you need a separate path for functions that returns void:

                // Call Objective-C code that may raise an exception here.
                static if (is(R == void)) func(objcArgs);
                else ObjcType!(R) objcResult = func(objcArgs);

                _NSRemoveHandler2(&_localHandler);
                
                // Converting return value.
                static if (is(R == void)) return;
                else return decapsulate!(R)(objcResult);

It could be rewriten in a simpler way if void variables were supported:

                // Call Objective-C code that may raise an exception here.
                ObjcType!(R) objcResult = func(objcArgs);

                _NSRemoveHandler2(&_localHandler);
                
                // Converting return value.
                return decapsulate!(R)(objcResult);

Note that returning a void resulting from a function call already works in D. You just can't "store" the result of such functions in a variable.

That said, it's not a big hassle in this case, thanks to static if. What suffers most is code readability.

--
Michel Fortin
michel.for...@michelf.com
http://michelf.com/

Reply via email to