After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 606480 - Add syntax-sugar for complex conditionals
Add syntax-sugar for complex conditionals
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: general
unspecified
Other All
: Low enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2010-01-09 11:56 UTC by pancake
Modified: 2010-03-21 21:21 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Support for chained conditionals (5.59 KB, patch)
2010-01-24 12:37 UTC, Marc-Andre Lureau
none Details | Review
Support for chained conditionals (5.18 KB, patch)
2010-01-24 12:43 UTC, Marc-Andre Lureau
none Details | Review

Description pancake 2010-01-09 11:56:46 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)) {
  ...
}
Comment 1 Marc-Andre Lureau 2010-01-24 12:37:48 UTC
Created attachment 152136 [details] [review]
Support for chained conditionals

Help bug 606480.
Comment 2 Marc-Andre Lureau 2010-01-24 12:43:14 UTC
damn configure.ac :) resending
Comment 3 Marc-Andre Lureau 2010-01-24 12:43:34 UTC
Created attachment 152137 [details] [review]
Support for chained conditionals

Help bug 606480.
Comment 4 pancake 2010-01-25 12:42:12 UTC
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? =)
Comment 5 Jürg Billeter 2010-01-29 18:44:43 UTC
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]) {
...
}
Comment 6 Marc-Andre Lureau 2010-01-29 21:17:01 UTC
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?
Comment 7 zarevucky.jiri 2010-01-29 21:18:15 UTC
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.
Comment 8 Marc-Andre Lureau 2010-02-04 11:23:46 UTC
it's also the number one hidden feature of python, in stackoverflow:

http://stackoverflow.com/questions/101268/hidden-features-of-python
Comment 9 Marc-Andre Lureau 2010-02-04 11:26:48 UTC
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
Comment 10 Marc-Andre Lureau 2010-02-04 11:27:34 UTC
(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 ;)
Comment 11 Jürg Billeter 2010-03-21 21:02:53 UTC
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.
Comment 12 Jürg Billeter 2010-03-21 21:21:16 UTC
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.