October 04, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tommi | Maybe we forget about commas then, and extend if-clauses so that you can properly define variables at the beginning of it. Separated by semicolons.
string name;
if (string street = nextStreet();
int number = nextNumber();
auto person = new Person(name);
person.livesAt(number, street))
{
// use street, number, and person
}
| |||
October 04, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, October 05, 2012 00:36:22 Adam D. Ruppe wrote:
> On Thursday, 4 October 2012 at 22:28:24 UTC, David Nadlinger
>
> wrote:
> > how often have you really encountered big syntactic headaches because of not having something like this available?
>
> I do somewhat regularly. The if(auto x = y()) { use x } is pretty
> convenient but being limited only to the bool check is kinda weak.
Yeah. It would definitely be useful to be able to do like you do with a for loop with an if, but in that case, I'd probably suggest just making it look like it looks like with for.
if(auto x = y(); x != 42)
{}
That would be really cool, but I expect that it would be hard to talk Walter into it.
- Jonathan M Davis
| |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tommi | Tommi:
> Maybe we forget about commas then, and extend if-clauses so that you can properly define variables at the beginning of it. Separated by semicolons.
Regarding definition of variables in D language constructs, there is one situation where sometimes I find D not handy. This code can't work:
do {
const x = ...;
} while (predicate(x));
You need to use:
T x;
do {
x = ...;
} while (predicate(x));
Bye,
bearophile
| |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Friday, October 05, 2012 02:08:14 bearophile wrote:
> Tommi:
> > Maybe we forget about commas then, and extend if-clauses so that you can properly define variables at the beginning of it. Separated by semicolons.
>
> Regarding definition of variables in D language constructs, there is one situation where sometimes I find D not handy. This code can't work:
>
> do {
> const x = ...;
> } while (predicate(x));
>
>
> You need to use:
>
> T x;
> do {
> x = ...;
> } while (predicate(x));
Yeah. That comes from C/C++ (and is the same in Java and C#, I believe). I don't know why it works that way. It's definitely annoying.
Of course, changing it at this point would change the semantics in a potentially code-breaking manner in that if the condition relies on any variables local to the loop having been destroyed, then its behavior will change. That's probably an insanely uncommon situation though - enough so that I'd be all for changing the semantics to have the scope exited _after_ the test is done (assuming that there's not a solid technical reason to keep it as-is). But I have no idea how possible it is to talk Walter into that sort of change.
- Jonathan M Davis
| |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Is the plan to deprecate comma operator for chaining expressions?
I would love to see more syntactic sugar to support tuples, and comma operator would be the best fit for that purpose.
eg:
----
import std.typecons;
auto fun(){
return tuple(1,"abc");
//1) ideally, we should be able to write:
//return (1,"abc");
//with same semantics (and no need to import std.typecons)
}
//at the call site: currently:
auto t=fun();
auto a=t[0];
auto b=t[1];
//2) ideally, we should be able to write:
auto (a,b,c)=fun();
//3) or even:
(a,b,c)=fun();
----
Will it be difficult to implement 2)? (by far the most important of 1,2,3)
Is 1) and 3) a good idea?
| |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to timotheecour | On Friday, October 05, 2012 02:33:45 timotheecour wrote: > Is the plan to deprecate comma operator for chaining expressions? That's all the comma operator does. If it's not chaining expressions, it's not the comma operator (e.g. variables declarations do _not_ use the comma operator even though they can use commas). There is a proposal to remove the comma operator, altering for loops so that they explicitly support using commas like they currently do but otherwise completely removing the comma operator: http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP19 > I would love to see more syntactic sugar to support tuples, and comma operator would be the best fit for that purpose. That's one of the main reasons for the proposal, but nothing has been decided yet. The discussion is here: http://forum.dlang.org/thread/k3ns2a$1ndc$1@digitalmars.com - Jonathan M Davis | |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Friday, 5 October 2012 at 00:22:04 UTC, Jonathan M Davis wrote:
> On Friday, October 05, 2012 02:08:14 bearophile wrote:
>> Tommi:
>> > Maybe we forget about commas then, and extend if-clauses so
>> > that you can properly define variables at the beginning of it.
>> > Separated by semicolons.
>>
>> Regarding definition of variables in D language constructs, there
>> is one situation where sometimes I find D not handy. This code
>> can't work:
>>
>> do {
>> const x = ...;
>> } while (predicate(x));
>>
>>
>> You need to use:
>>
>> T x;
>> do {
>> x = ...;
>> } while (predicate(x));
Don't forget the with statement, it's not "just" for switches! In many cases it's actually even better than the proposed changes _and_ it works today!
import std.stdio;
struct d_is_beautiful
{
int a=1;
int b=2;
}
void main()
{
with(d_is_beautiful()) if(a==1)
writeln("ok");
else
writeln("ko:", a);
with(d_is_beautiful()) do
{
++a;
writeln("iter");
}
while(a!=b);
}
| |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tommi | On 10/04/2012 11:56 PM, Tommi wrote:
> On Thursday, 4 October 2012 at 21:32:34 UTC, Jonathan M Davis wrote:
>>
>> If you want to restrict the scope of a variable, you can simply use
>> another set of braces to create a new scope. It might be more verbose
>> than desirable, but it works just fine. e.g.
>>
>> {
>> int n = getInt();
>> if(n > 10)
>> {
>> ...
>> }
>> }
>
> But if there are else-if clauses, then you end up polluting your
> namespace, and notice how the syntax of your workaround deteriorates
> exponentially:
> ...
Check your math.
| |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tove | On Friday, 5 October 2012 at 07:26:43 UTC, Tove wrote:
> Don't forget the with statement, it's not "just" for switches! In many cases it's actually even better than the proposed changes _and_ it works today!
I've asked recently to have declaration inside the with statement too (same as if has now)... I don't remember why exactly now but it definitely seemed useful at the time.
| |||
October 05, 2012 Re: Feature request: extending comma operator's functionality | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tove | On Friday, 5 October 2012 at 07:26:43 UTC, Tove wrote: > Don't forget the with statement, it's not "just" for switches! In many cases it's actually even better than the proposed changes _and_ it works today! > > import std.stdio; > > struct d_is_beautiful > { > int a=1; > int b=2; > } > > void main() > { > with(d_is_beautiful()) if(a==1) > writeln("ok"); > else > writeln("ko:", a); > > with(d_is_beautiful()) do > { > ++a; > writeln("iter"); > } > while(a!=b); > } This was news to me. It seems you can also use a tuple for that. That's a pretty decent workaround: import std.typecons; //... with (Tuple!(int, "a")(getInt())) if (a > 9) { //... } else with (Tuple!(char, "b")(getChar())) if (b == 'D') { //... } On Friday, 5 October 2012 at 07:28:29 UTC, Timon Gehr wrote: >> But if there are else-if clauses, then you end up polluting your namespace, and notice how the syntax of your workaround deteriorates exponentially: >> ... > > Check your math. Correction: "the syntax deteriorates only linearly". | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply