Re: [Vala] Two small new features
On Wed, Sep 29, 2010 at 00:42:55 +1000, Fredderic Unpenstein wrote: > On 28 September 2010 21:19, Julian Andres Klode wrote: > > On Mo, 2010-09-27 at 02:55 +0200, Frederik Sdun wrote: > >> This is just for convinience, for the not unusual case of: > >> if (args.length > 0) > >> foreach(...) > >> else > >> ... > > Personally I find myself, in almost every language, wrapping for(each) > statements in "if (the list has items) ... else ...", which is exactly > what this feature addresses. And, it's essentially saying exactly > what such a structure looks like; do this loop, else do this other > thing if the loop never happened (because there weren't any items). IMHO confusing and not really needed, because the if above is not that much more writing. > > That would be confusing for people with Python background, as 'else' is > > executed when the loop is exhausted; that is if there was no > > return/break in the loop body. > > That doesn't strike me as particularly useful, or as a particularly > good definition of "else" for that matter. I'd consider that more of > a "then" situation, with "else" used because it's already a handy > reserved keyword that they couldn't think of a better use for. Except, well, it's not. It is really else for cases when the loop is looking for something. As in: for x in cache: if want(x): break else: cache += new_entry(), > Lets not punish Vala for Python's lack of good taste, though "then" as > in "Python's for...else" could be handy too. ;) In fact I can see more use for the python case. As I said, writing the if(no-elements) before the loop is easy and makes your intention clear. On the other hand I've seen lot's of: for(i = begin; i != end; ++i) { if(something(i)) { found = true; break; } } if(!found) something_else(); which is pretty ugly. If you move the variable out of the for initialization, and provided the condition has no side-effects, be rewritten as: for(i = begin; i != end; ++i) if(something(i)) break; if(i != end) ; // nothing else // here is our little ELSE something_else(); I believe that making else shortcut for this with the benefits that - the else branch is in i's scope even if i is declared in the loop initialization - and the end codnition is not evaluated when it does not need to be is both more obvious and more useful. On a side note, I do like the else for try/catch. I'd say it's much more a "than" situation there (because it's executed if the try succeeded), but else is already being used for that purpose in other languages (python among other). -- Jan 'Bulb' Hudec ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Two small new features
On 28 September 2010 21:19, Julian Andres Klode wrote: > On Mo, 2010-09-27 at 02:55 +0200, Frederik Sdun wrote: >> This is just for convinience, for the not unusual case of: >> if (args.length > 0) >> foreach(...) >> else >> ... Personally I find myself, in almost every language, wrapping for(each) statements in "if (the list has items) ... else ...", which is exactly what this feature addresses. And, it's essentially saying exactly what such a structure looks like; do this loop, else do this other thing if the loop never happened (because there weren't any items). Absolutely fantastic that a useful language has this! > That would be confusing for people with Python background, as 'else' is > executed when the loop is exhausted; that is if there was no > return/break in the loop body. That doesn't strike me as particularly useful, or as a particularly good definition of "else" for that matter. I'd consider that more of a "then" situation, with "else" used because it's already a handy reserved keyword that they couldn't think of a better use for. Lets not punish Vala for Python's lack of good taste, though "then" as in "Python's for...else" could be handy too. ;) ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Two small new features
On Mo, 2010-09-27 at 02:55 +0200, Frederik Sdun wrote: > * Jürg Billeter [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 > ... That would be confusing for people with Python background, as 'else' is executed when the loop is exhausted; that is if there was no return/break in the loop body. Python documentation of 'for' = When the items are exhausted (which is immediately when the sequence is empty), the suite in the ``else`` clause, if present, is executed, and the loop terminates. A ``break`` statement executed in the first suite terminates the loop without executing the ``else`` clause's suite. A ``continue`` statement executed in the first suite skips the rest of the suite and continues with the next item, or with the ``else`` clause if there was no next item. -- Julian Andres Klode - Debian Developer, Ubuntu Member See http://wiki.debian.org/JulianAndresKlode and http://jak-linux.org/. ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Two small new features
* Jürg Billeter [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: pla...@draugr.de identi.ca: playya signature.asc Description: Digital signature ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Two small new features
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 ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
[Vala] Two small new features
Moin, 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); } 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); } ATM datatype using iterators don't work, but if you like the feature, I'll implement it. Same for "for...else". Regards, Frederik P.S.: There's a another branch, which supports async delegates/lambdas at [1] which requires some testing/reviewing. [0]: http://git.freesmartphone.org/?p=vala.git;a=shortlog;h=refs/heads/else-statements [1]: http://git.freesmartphone.org/?p=vala.git;a=shortlog;h=refs/heads/async -- IRC: playya @ Freenode, Gimpnet xmpp: pla...@draugr.de identi.ca: playya signature.asc Description: Digital signature ___ vala-list mailing list vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list