[Vala] Suggestion: first {} and last {} operators in iteration-loops



Hello there,

a few days ago I sent this suggestion to Jürg. Since I got no answer
yet, I feel free to send it here. ;)  Of course I think it's a good
idea, but I you think that I've been smoking pot, just tell me so. :D

Have you ever considered first{} and last{} constructs for Vala?

I noticed that in a lot of cases when iterating over lists, the first
and the last element of such lists get special treatment. In c, you
usualy identify those elements manualy. And while its certainly possible to
use the same methods in Vala (or Java, or C#), it does strike me as
very clumsy and somewhat wrong.

Because of that I'd like to suggest first{} and last{} operators for
iteration loops:

/* This is the way you'd do it currently */
static int main (string[] args) {
         int a = 0;

        foreach (string arg in args) {
                    if(0 = a) {
                         stdout.printf("%s is the first string", arg);
                    } else if (a = args.length() -1 ) {
                         stdout.printf("%s is the last string", arg);
                    } else {
                          stdout.printf("%s is a string", arg);
                    }
                    a++;
           }
           return 0;
}

As you can see it's rather gruesome and not very easy to read. Try the
following:

/* With first{} and last{} */
static int main (string[] args) {

        foreach (string arg in args) {

                    stdout.printf("%s is a string", arg);

                    first {
                         stdout.printf("%s is the first string", arg);
                    }
                    last {
                         stdout.printf("%s is the last string", arg);
                    }
           }
           return 0;
}


As you can see, it's much easier to read. And the Vala pre-compiler
could also make use of the internal for-each iterator, making it more
efficient also.

Of course, this will break the language by introducing two new
keywords. And it will also move Vala a little bit further away from C#.
But I still think it would be worth it. If someone were to mentor me, I'd
even do it myself.

So long,
Raphael


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