Re: [Vala] Two small new features
- From: Jürg Billeter <j bitron ch>
- To: Frederik Sdun <frederik sdun googlemail com>
- Cc: Vala <vala-list gnome org>
- Subject: Re: [Vala] Two small new features
- Date: Sat, 25 Sep 2010 18:22:16 +0200
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.
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");
}
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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]