www.digitalmars.com         C & C++   DMDScript  

D - Labeling Blocks

reply "anderson" <anderson firestar.com.au> writes:
<babble>

Ada allowed block of code to be labelled so that variables with the same
name could be explicitly called. Does D do anything like this? My own
opinion is that it's not really nessary in classes if you have these rules:

1) A variable name cannot be reused in the same function/method, but can be
the same a global class member variable.
2) Use the "this" tool to access class member variables when there are
duplicate names.

Parhaps D already has simular rules (I guess I should test it).

However, if people like reusing variable names in blocks then parhaps
something like this could be used.

{
    int x;  //x 1
    {
        prev.x = ... //Access x 1

        int x;
        {
            prev.prev.x = ... //Access x 1
        }
    }
}

or Blocks could be named


Block1
{
    int x;

    Block2
    {
        Block1.x = ... //Access x 1

        int x;

        Block3
        {
            Block1.x = ... //Access x 1
        }
    }
}

I personally can't see any use for reusing varable names in the same
functional space. Why ADA of all languages (strongly typed as it is) allowed
for this amazes me.
Jul 23 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Tue, 23 Jul 2002 17:31:10 +0800 "anderson" <anderson firestar.com.au> wrote:

 1) A variable name cannot be reused in the same function/method, but can be
 the same a global class member variable.

It is already so. No local variable can hide another local variable.
 2) Use the "this" tool to access class member variables when there are
 duplicate names.

Yep.
Jul 23 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374606384730556 news.digitalmars.com...
 On Tue, 23 Jul 2002 17:31:10 +0800 "anderson" <anderson firestar.com.au>

 1) A variable name cannot be reused in the same function/method, but can


 the same a global class member variable.

It is already so. No local variable can hide another local variable.
 2) Use the "this" tool to access class member variables when there are
 duplicate names.

Yep.

But you can do this right? void func() { { int i = 10; while (i > 0) { printf ("i == %d\n", i); i--; } } { int i = 0; while (i < 10) { printf ("i == %d\n", i); i++; } } } -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Jul 24 2002
parent "Walter" <walter digitalmars.com> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:ahmjmf$1d8$1 digitaldaemon.com...
 But you can do this right?

 void func()
 {
     {
         int i = 10;
         while (i > 0)
         {
             printf ("i == %d\n", i);
             i--;
         }
     }
     {
         int i = 0;
         while (i < 10)
         {
             printf ("i == %d\n", i);
             i++;
         }
     }
 }

Yes, just not: void func() { int i; { int i; } } The reason is that most of the time that was a mistake, I know I have been caught by it more than once.
Jul 24 2002