Re: [Vala] Two small new features



* Jürg Billeter <j bitron ch> [25.09.2010 18:22]:
Hi Frederik,

On Sat, 2010-09-25 at 17:20 +0200, Frederik Sdun wrote:
i just pushed two little patches to [0] which allows to add an else
statement to foreach and catch statements.
For foreach, the else block is called if there is no iteration in the
loop.
For the catch clause, the else block is called if no exception was
caught and before the finally statement.

Here are 2 little examples:
foreach:
void print_array (string[] args) {
        foreach (var arg in args) {
                debug(@"arg: $arg");
        } else {
                debug("no args"); //called if the array is empty
        }
}
void main (string[] args)
{
        string[] test_1 = new string[0];
        string[] test_2 = new string[2]{"hello", "world"};
        print_array (test_1);
        print_array (test_2);
}

The `else` seems to indicate that the code in the else block is reached
if the foreach is not successful. However, zero iterations are by no
means an unusual condition. In general, I'd like to keep (control flow)
statements relatively simple as, in my opinion, it's ok that the code
gets more complex when the control flow is more complex. You don't want
to hide complex control flow.

This is just for convinience, for the not unusual case of:
if (args.length > 0)
    foreach(...)
else
    ...
catch:
public errordomain TestError {
        FOO,
}
public void throw_error (bool t) throws TestError {
        if (t) {
             throw new TestError.FOO("foo");
        }
}
void test(bool t) {
        try { 
            throw_error (t);
        } catch (TestError e) {
            debug("error :(");
        } else {
            debug("success :)");
        } finally {
            debug("finally");
        }
}
void main(){
        debug("false:");
        test(false);
        debug("true:");
        test(true);
}

What is the difference to the following?

    try {
        throw_error (t);
        debug ("success :)");
    } catch (TestError e) {
        debug("error :(");
    } finally {
        debug("finally");
    }


You are outside of the try block, which allows you to throw exceptions,
which should be handled in an outer scope.

P.S.: There's a another branch, which supports async delegates/lambdas at [1]
which requires some testing/reviewing.

I definitely like that one. I'll try to review it as soon as possible.

Thanks,
Jürg


-- 
IRC: playya @ Freenode, Gimpnet
xmpp: playya draugr de
identi.ca: playya

Attachment: signature.asc
Description: Digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]