GNOME Bugzilla – Bug 606480
Add syntax-sugar for complex conditionals
Last modified: 2010-03-21 21:21:16 UTC
perl6, python and many other languages supports complex comparisions that simplifies the reading of the code. A conditional like this: if (1 < a < 10) { print ("It's in!\n"); } equals to: if ((1<a) && (a<10)) { ... }
Created attachment 152136 [details] [review] Support for chained conditionals Help bug 606480.
damn configure.ac :) resending
Created attachment 152137 [details] [review] Support for chained conditionals Help bug 606480.
Good work! :) I have made some more tests a part from the given example and it seems to work correctly. It would be nice to have this in mainstream. Somebody to review and commit? =)
Thanks for the patch. However, I'm not sure yet whether this should be added to Vala. There are alternatives for the same functionality that we might want to consider as well, for example: if (a in [2..9]) { ... }
Would that be supported too: "a in [min()..max()]". It looks more pleasant to me to read "min() < a < max ()". Also why we couldn't have both?
For me, operator chaining feels more natural than using ranges. Also, it's much more flexible, for example: if (0 < a < b <= c < 10) { // do something } else { // report invalid input data } You couldn't do this with ranges. Another thing is that there is no other use of ranges in the language (or is there?). Adding them just for this doesn't feel right.
it's also the number one hidden feature of python, in stackoverflow: http://stackoverflow.com/questions/101268/hidden-features-of-python
And the second best hidden feature of C# is chaining ??, which I believe should work with this patch. string result = value1 ?? value2 ?? value3 ?? String.Empty; http://stackoverflow.com/questions/9033/hidden-features-of-c
(In reply to comment #9) > And the second best hidden feature of C# is chaining ??, which I believe should > work with this patch. > hmm perhaps not, need to try that ;)
I'm planning to add this to Vala as experimental syntax. The patch does work fine for simple cases, however, it appears to break if you chain more than two operations together. Also, the parser part could probably be simplified, the recursive function looks like overkill as we already have a loop with a very similar purpose. I'll check whether I can fix this quickly.
commit 4f0216a58bcf0b37ed96d7b45f40cca3de2d083e Author: Jürg Billeter <j@bitron.ch> Date: Sun Jan 24 13:37:49 2010 +0100 Add experimental support for chained relational expressions Based on patch by Marc-André Lureau, fixes bug 606480.