Jump to page: 1 2 3
Thread overview
[Suggestion] DeclarationStatement as Expression.
Aug 10, 2005
AJG
Aug 10, 2005
Derek Parnell
Aug 10, 2005
AJG
Aug 10, 2005
AJG
Aug 10, 2005
Chris Sauls
Aug 10, 2005
Deewiant
Aug 10, 2005
AJG
Aug 10, 2005
Ben Hinkle
Aug 10, 2005
AJG
Aug 10, 2005
xs0
Aug 11, 2005
AJG
Aug 11, 2005
xs0
Aug 12, 2005
AJG
Aug 10, 2005
Ben Hinkle
Aug 10, 2005
AJG
Aug 11, 2005
Ben Hinkle
Aug 11, 2005
Ben Hinkle
Aug 11, 2005
AJG
Aug 11, 2005
Ben Hinkle
Aug 11, 2005
Stewart Gordon
Aug 11, 2005
Ben Hinkle
Aug 11, 2005
AJG
Aug 11, 2005
AJG
Aug 11, 2005
Ben Hinkle
Aug 11, 2005
AJG
Aug 12, 2005
Ben Hinkle
Aug 12, 2005
Stewart Gordon
August 10, 2005
Hi there,

What do you think? The idea is for the statement to inherit the scope of the construct or block it is declared in. Similar to how a for loop does it:

# for (int i = 0; i < length; ++i) Foo(i);

In addition, the expression inherits the type and value of the declaration itself. For instance:

# if ((string line = ReadLine()) != null) {
#      printf("We got a line, Chief!");
#      line = EscapeLine(line);
#      DeployLine(line);
# } else {
#      printf("We are out of lines, Chief!");
#      line = "Emergency Line";
#      DeployLine(line);
# }

I see it as a simple extension of what's already available with assignment
expressions. Now, would this be possible? What are the disadvantages?
I don't think this would break any code.

Cheers,
--AJG.








August 10, 2005
On Wed, 10 Aug 2005 02:44:01 +0000 (UTC), AJG wrote:

> Hi there,
> 
> What do you think? The idea is for the statement to inherit the scope of the construct or block it is declared in. Similar to how a for loop does it:
> 
> # for (int i = 0; i < length; ++i) Foo(i);
> 
> In addition, the expression inherits the type and value of the declaration itself. For instance:
> 
> # if ((string line = ReadLine()) != null) {
> #      printf("We got a line, Chief!");
> #      line = EscapeLine(line);
> #      DeployLine(line);
> # } else {
> #      printf("We are out of lines, Chief!");
> #      line = "Emergency Line";
> #      DeployLine(line);
> # }
> 
> I see it as a simple extension of what's already available with assignment
> expressions. Now, would this be possible? What are the disadvantages?
> I don't think this would break any code.
> 

I remember this being suggested so time ago. It could be a nice addition. In the meantime, will this do ... ?

{ string line;
if ((line = ReadLine()) != null) {
     printf("We got a line, Chief!");
     line = EscapeLine(line);
     DeployLine(line);
} else {
     printf("We are out of lines, Chief!");
     line = "Emergency Line";
     DeployLine(line);
}
}

This limits the scope of the declaration.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
10/08/2005 12:56:09 PM
August 10, 2005
Hi there,

I forgot to add something.
If the statement/expression contains multiple declarations, like:
int i = 1, j = 2, k = 3;

Then my suggestion would work just like the Comma operator currently does. It makes the expression equal to the very last declaration.

# // Currently allowed:
# int foo = (1, 2, 3);
# // foo == 3.

# // My suggestion would allow:
# int foo = (int i = 1, j = 2, k = 3);
# // foo == 3.

Cheers,
--AJG.


August 10, 2005
>I remember this being suggested so time ago. It could be a nice addition.

Yay! First supporter.

>In the meantime, will this do ... ?
>
>{ string line;
>if ((line = ReadLine()) != null) {
>     printf("We got a line, Chief!");
>     line = EscapeLine(line);
>     DeployLine(line);
>} else {
>     printf("We are out of lines, Chief!");
>     line = "Emergency Line";
>     DeployLine(line);
>}
>}
>
>This limits the scope of the declaration.

The scope limitation is not the main benefit to me, but rather that the whole thing becomes nicely streamlined. Oh, and the other neg. side effect is Indentation++. You kinda cheated. ;)

Cheers,
--AJG.


August 10, 2005
AJG wrote:
> Hi there,
> 
> What do you think?

I think it could be useful.  Especially if it could be made to work with 'while()' statements and their ilk.  Perhaps via an implied 'static' attribute.

-- Chris Sauls
August 10, 2005
AJG wrote:
> Hi there,
> 
> What do you think? The idea is for the statement to inherit the scope of the construct or block it is declared in. Similar to how a for loop does it:
> 
> # for (int i = 0; i < length; ++i) Foo(i);
> 
> In addition, the expression inherits the type and value of the declaration itself. For instance:
> 
> # if ((string line = ReadLine()) != null) {
> #      printf("We got a line, Chief!");
> #      line = EscapeLine(line);
> #      DeployLine(line);
> # } else {
> #      printf("We are out of lines, Chief!");
> #      line = "Emergency Line";
> #      DeployLine(line);
> # }
> 
> I see it as a simple extension of what's already available with assignment
> expressions. Now, would this be possible? What are the disadvantages?
> I don't think this would break any code.
> 
> Cheers,
> --AJG.
> 

I take it this would also allow multiple definitions in the Initializer part of a ForStatement? I.e.:

for (int i = 0, int j = 2;;)

If so, that's another plus, at least for me.
August 10, 2005
Hi,

>I take it this would also allow multiple definitions in the Initializer part of a ForStatement? I.e.:
>
>for (int i = 0, int j = 2;;)

Yep.

>If so, that's another plus, at least for me.

That would be nice indeed.

Cheers,
--AJG.


August 10, 2005
"AJG" <AJG_member@pathlink.com> wrote in message news:ddbplh$17c3$1@digitaldaemon.com...
> Hi there,
>
> What do you think? The idea is for the statement to inherit the scope of
> the
> construct or block it is declared in. Similar to how a for loop does it:
>
> # for (int i = 0; i < length; ++i) Foo(i);
>
> In addition, the expression inherits the type and value of the declaration itself. For instance:
>
> # if ((string line = ReadLine()) != null) {
> #      printf("We got a line, Chief!");
> #      line = EscapeLine(line);
> #      DeployLine(line);
> # } else {
> #      printf("We are out of lines, Chief!");
> #      line = "Emergency Line";
> #      DeployLine(line);
> # }
>
> I see it as a simple extension of what's already available with assignment
> expressions. Now, would this be possible? What are the disadvantages?
> I don't think this would break any code.
>
> Cheers,
> --AJG.

are the following valid?
if (bar(x,int x = 1))
if (0 || int x = 1)

In other words, what exactly are the semantics of the proposal?


August 10, 2005
Hi,

>are the following valid?
>if (bar(x,int x = 1))
>if (0 || int x = 1)

In theory, yes. It's possible that due to some ambiguity the parsing may require an extra set of parentheses:

# if (bar(x, (int x = 1)))
# if (0 || (int x = 1))

Also, I'm not sure if the two x vars would conflict. The docs say they _would_, but the compiler says they wouldn't. That would be up for discussion.

>In other words, what exactly are the semantics of the proposal?

Well, the premise is that there are many places where an inline declaration makes sense and is very convenient. The for loop is such an example. My idea is to extend this.

Of course, your examples are trivial, but consider something like:

# if (bar(42, int x = rand())) foo(x);

Or something like:

# if (0 || (int x = SideEffect()) > 5 && x < 10) bar(x);

Anyway, this concept is not radical. It can already be done in PHP, Perl, and various other languages. I think it would be fantastic to bring it over to our corner of the world with the added benefits of strong typing.

Cheers,
--AJG.



August 10, 2005
Well, Walter already said he likes the idea:

http://www.digitalmars.com/d/archives/digitalmars/D/20481.html

Hopefully that means it will get implemented eventually :) BTW, when would you need this outside if/for/while? Using local declarations in those has the benefit of scoping them locally to the loop, while using them randomly in normal statements just makes the code harder to read, imho..


Cheers,
xs0

AJG wrote:
> Hi,
> 
> 
>>are the following valid?
>>if (bar(x,int x = 1))
>>if (0 || int x = 1)
> 
> 
> In theory, yes. It's possible that due to some ambiguity the parsing may require
> an extra set of parentheses:
> 
> # if (bar(x, (int x = 1)))
> # if (0 || (int x = 1))
> 
> Also, I'm not sure if the two x vars would conflict. The docs say they _would_,
> but the compiler says they wouldn't. That would be up for discussion.
> 
> 
>>In other words, what exactly are the semantics of the proposal? 
> 
> 
> Well, the premise is that there are many places where an inline declaration
> makes sense and is very convenient. The for loop is such an example. My idea is
> to extend this.
> 
> Of course, your examples are trivial, but consider something like:
> 
> # if (bar(42, int x = rand())) foo(x);
> 
> Or something like:
> 
> # if (0 || (int x = SideEffect()) > 5 && x < 10) bar(x);
> 
> Anyway, this concept is not radical. It can already be done in PHP, Perl, and
> various other languages. I think it would be fantastic to bring it over to our
> corner of the world with the added benefits of strong typing.
> 
> Cheers,
> --AJG.
> 
> 
> 
« First   ‹ Prev
1 2 3