Chris Browne: Anyone know of any languages...
cjbrowne:
liamgoodacre:
cjbrowne:
Which use the comma operator a bit more usefully than in C/C++ and allow you to do this sort of wizardry:
(foo,bar).baz(); // runs the method ‘baz()’ first on ‘foo’ then on ‘bar’, illegal if either ‘foo’ or ‘bar’ do not have a method ‘baz()’ but legal even if ‘foo’ and ‘bar’ are different types
I’m experimenting with language design and would be interested in opinions on this syntax (I can see a lot of ways in which it could go horribly wrong, and implementations would have to be careful about operator precedence, but I think as long as the comma-operator has the absolute lowest precedence of all operators, it should work fine)
I think it might make more sense to say that (foo, bar) is a sequence, and the dot operator maps the call over all the elements. This prevents the comma from being an operator, and just a delimiter for the sequence.
Have a look into Array Programming languages. Although I don’t know of any Object Oriented Array Programming Languages, here is a paper I just found about it High Level OOP
I too am experimenting with language design, although less on syntax more on semantics. I might start posting about my ideas too. I was also just thinking about array programming when I saw your post!
Hope to hear more about your experiments.
Well, this was just a specific case; in the more general case I want to treat it “like” an operator that just evaluates the expressions on either side of it (left first) and returns both evaluated expressions to the statement (unlike other operators which only return one result). It would cause the operator to create a branch in the code path, like an if() statement does only both branches are guaranteed to be executed (left-side first).
So you could also embed it into an if statement, for some even funkier effects:
if(foo,bar) { print(“something was true”); }
expands out into:
if(foo) { print(“something was true”); } if(bar) { print(“something was true”); }
The problem is that programmers unfamiliar with this style might be confused and not realise that both expressions will be evaluated, and think that using it in an if statement like above will just be an alias for the || or && operators. The advantage, though, is that you can do the even more wizardly:
if(baz = (foo,bar)) { print(baz.toString()); print(” evaluates to true!”); }
Which would basically be syntactic sugar for:
if(foo) { print(foo.toString()); print(” evaluates to true!”); }
if(bar) { print(bar.toString()); print(” evaluates to true!”); }
Is this a good feature or a bad one, though? I know I would use it a lot but that probably suggests it’s a bad feature… :P
The strange thing here is that an expression is used as a control flow for statements, which may get rather unusual on the semantics side of things. The following would be roughly equivalent to your last example:
for (baz in [foo, bar]) {
if (baz) {
print(baz.toString());
print(" evaluates to true!");
}
}
But reguardless of that, it’s still interesting to think about, so continuing with the idea…
You could do some weird things like combining the comma operator with a double question mark to filter based on a method call:
print((foo, bar ?? validBaz()).toString());
Or maybe:
print((zuux <- foo, bar ?? zuux.validBaz()).toString());
Expanding to:
for (zuux in [foo, bar]) {
if (zuux.validBaz()) {
print(zuux.toString());
}
}