www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A scoping operator

reply "Matthew" <matthew.hat stlsoft.dot.org> writes:
One of the things I've wanted in C++ for a long time is the ability to be able
to
use scope in a similar vein to file-system location. In other words, I would
like
to be able to have the following

    ::X - refers to X in global scope
    :X - refers to X in local scope
    ::::X - refers to X in next most local scope

(only with better thought-out syntax)

What I'm interested in are people's thoughts for whether this would be nice in
D,
and suggestions for syntax.
May 01 2004
next sibling parent Roel Mathys <roel.mathys yucom.be> writes:
Matthew wrote:

 One of the things I've wanted in C++ for a long time is the ability to be able
to
 use scope in a similar vein to file-system location. In other words, I would
like
 to be able to have the following
 
     ::X - refers to X in global scope
     :X - refers to X in local scope
     ::::X - refers to X in next most local scope
 
 (only with better thought-out syntax)
 
 What I'm interested in are people's thoughts for whether this would be nice in
D,
 and suggestions for syntax.
 
 
 
I think ':' and "::" are not used for the moment in D, maybe there are other uses for these? e.g. slicing ;-) bye roel
May 01 2004
prev sibling next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Doesn't . refer to global scope?

Couldn't we just extend that?

. //Global scope
.. //Next scope under the global scope
//and so on

ie

int a; //global a

class A
{
 int a;

 void  A
 {
      .a; //refers to global a
      ..a; //refers to A.a
 }

}

and

: //Next most available scope that has the variable name (not including 
the current scope)
:: //Next most available scope that has the variable name (not including 
the current scope, or the next most outer scope)
//and so on

int a; //global a

class A
{
 static int a;

 struct X
 {
   int a;
   struct Y
   {
     void  A
     {
          a; //refers to A.X.a
          :a; //refers to A.X.a //If struct Y was removed it would refer 
to A.a
          ::a; //refers to A.a
          :::a; //refers to global a
       }
    }
 }
}

I guess the above could be changed to something like:

: //Next scope (not including the current scope)
:: //Next scope (not including the current scope, or the next most outer 
scope)
//and so on

Which could help prevent errors.


That way you can go both ways (from the global and from the local).

PS - Sorry for posting to your personal email.  Dam mozilla. - 3rd time 
lucky <g>

-- 
-Anderson: http://badmama.com.au/~anderson/
May 01 2004
parent reply "Scott Egan" <scotte tpg.com.aux> writes:
Is there actually such a thing as global scope?  From what I can tell the
highest level of scope is module.

Or is that what you mean?

I'm not trying to be smart, just want to be correct.

This thread is relevant to my 'Gripe about 'with'' thread.

Another reason to use '::' would be to free up '.' for use in 'with'.  Which
if nothing else improves readability because it is obvious where the
reference is comming from.  (I found the use of just the varible names in
the with block a bit uncomfortable).




"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c714f0$1guj$1 digitaldaemon.com...
 Doesn't . refer to global scope?

 Couldn't we just extend that?

 . //Global scope
 .. //Next scope under the global scope
 //and so on

 ie

 int a; //global a

 class A
 {
  int a;

  void  A
  {
       .a; //refers to global a
       ..a; //refers to A.a
  }

 }

 and

 : //Next most available scope that has the variable name (not including
 the current scope)
 :: //Next most available scope that has the variable name (not including
 the current scope, or the next most outer scope)
 //and so on

 int a; //global a

 class A
 {
  static int a;

  struct X
  {
    int a;
    struct Y
    {
      void  A
      {
           a; //refers to A.X.a
           :a; //refers to A.X.a //If struct Y was removed it would refer
 to A.a
           ::a; //refers to A.a
           :::a; //refers to global a
        }
     }
  }
 }

 I guess the above could be changed to something like:

 : //Next scope (not including the current scope)
 :: //Next scope (not including the current scope, or the next most outer
 scope)
 //and so on

 Which could help prevent errors.


 That way you can go both ways (from the global and from the local).

 PS - Sorry for posting to your personal email.  Dam mozilla. - 3rd time
 lucky <g>

 -- 
 -Anderson: http://badmama.com.au/~anderson/
May 01 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Scott Egan wrote:

(I found the use of just the varible names in the with block a bit
uncomfortable).
  
You've had to much VB. -- -Anderson: http://badmama.com.au/~anderson/
May 01 2004
next sibling parent "Scott Egan" <scotte tpg.com.aux> writes:
Is that the same as too soft ;)


"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c71eva$228o$1 digitaldaemon.com...
 Scott Egan wrote:

(I found the use of just the varible names in the with block a bit
uncomfortable).

 You've had to much VB.

 -- 
 -Anderson: http://badmama.com.au/~anderson/
May 01 2004
prev sibling next sibling parent reply J C Calvarese <jcc7 cox.net> writes:
J Anderson wrote:
 Scott Egan wrote:
 
 (I found the use of just the varible names in the with block a bit 
 uncomfortable).
  
You've had to much VB.
Better too much VB than too much JScript. ;) -- Justin http://jcc_7.tripod.com/d/
May 01 2004
parent reply "Unknown W. Brackets" <unknown at.simplemachines.dot.org> writes:
J C Calvarese wrote:
 
 Better too much VB than too much JScript. ;)
 
Yes, JScript (being Microsoft's proprietary version of JavaScript) is perhaps a worse evil than Visual Basic or VBScript. Comparing Visual Basic and JavaScript is not fair, because they have not only totally different purposes but one is also compiled (mostly) and the other isn't. However, JavaScript compared to VBScript... is not a comparison. It is easy to see or prove that JavsScript is more often used, and much better supported. And, have you looked at this? http://www.digitalmars.com/dscript/ Looks like our good Walter prefers it too. (he didn't even mention VBScript on that page, did he?) I'm not surprised, because Visual Basic is... well, it's like making an RPG with RPG maker. It's not *really* making a game. But, these are just my opinions, and I have absolutely no intentions of arguing this point with you - especially because, frankly, there is not a single thing you could say to get me to say Visual Basic is a good programming language. -[Unknown]
May 01 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Unknown W. Brackets wrote:
 J C Calvarese wrote:
 
 Better too much VB than too much JScript. ;)
Yes, JScript (being Microsoft's proprietary version of JavaScript) is perhaps a worse evil than Visual Basic or VBScript. Comparing Visual Basic and JavaScript is not fair, because they have not only totally different purposes but one is also compiled (mostly) and the other isn't. However, JavaScript compared to VBScript... is not a comparison. It is easy to see or prove that JavsScript is more often used, and much better supported. And, have you looked at this? http://www.digitalmars.com/dscript/ Looks like our good Walter prefers it too. (he didn't even mention VBScript on that page, did he?) I'm not surprised, because Visual Basic is... well, it's like making an RPG with RPG maker. It's not *really* making a game. But, these are just my opinions, and I have absolutely no intentions of arguing this point with you - especially because, frankly, there is not a single thing you could say to get me to say Visual Basic is a good programming language. -[Unknown]
I've got no beef with ECMA script or JavaScript in particular. As far as Microsoft's proprietary languages, I prefer VBScript to JScript because I'm more confortable with BASIC syntax and it has a "for each". I guess seeing your mention of JavaScript in the "Gripes about 'with'" thread inspired me to make fun of its cousin JScript. -- Justin http://jcc_7.tripod.com/d/
May 02 2004
parent reply "Unknown W. Brackets" <unknown at.simplemachines.dot.org> writes:
J C Calvarese wrote:
 
 I've got no beef with ECMA script or JavaScript in particular. As far as 
 Microsoft's proprietary languages, I prefer VBScript to JScript because 
 I'm more confortable with BASIC syntax and it has a "for each".
JavaScript has: for (var key in array) alert(key + " -> " + array[key]); Which is essentially "for each". Works on associatives and arrays - which are essentially the same thing, mind you. -[Unknown]
May 02 2004
parent J C Calvarese <jcc7 cox.net> writes:
Unknown W. Brackets wrote:
 J C Calvarese wrote:
 
 I've got no beef with ECMA script or JavaScript in particular. As far 
 as Microsoft's proprietary languages, I prefer VBScript to JScript 
 because I'm more confortable with BASIC syntax and it has a "for each".
JavaScript has: for (var key in array) alert(key + " -> " + array[key]); Which is essentially "for each". Works on associatives and arrays - which are essentially the same thing, mind you. -[Unknown]
Thanks for the correction. -- Justin http://jcc_7.tripod.com/d/
May 02 2004
prev sibling parent reply "Phill" <phill pacific.net.au> writes:
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c71eva$228o$1 digitaldaemon.com...
 Scott Egan wrote:

(I found the use of just the varible names in the with block a bit
uncomfortable).

 You've had to much VB.
Is that Victorian Bitter? or Visual Basic? Or both? Phill
May 01 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Phill wrote:

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c71eva$228o$1 digitaldaemon.com...
  

Scott Egan wrote:

    

(I found the use of just the varible names in the with block a bit
      
uncomfortable).
      
You've had to much VB.
Is that Victorian Bitter? or Visual Basic? Or both? Phill
Doesn't matter, they both make you look stupid. I apologise, I just couldn't resist. -- -Anderson: http://badmama.com.au/~anderson/
May 01 2004
next sibling parent "Scott Egan" <scotte tpg.com.aux> writes:
LOL to both of you



"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c71ts1$2qr7$1 digitaldaemon.com...
 Phill wrote:

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c71eva$228o$1 digitaldaemon.com...


Scott Egan wrote:



(I found the use of just the varible names in the with block a bit
uncomfortable).

You've had to much VB.
Is that Victorian Bitter? or Visual Basic? Or both? Phill
Doesn't matter, they both make you look stupid. I apologise, I just couldn't resist. -- -Anderson: http://badmama.com.au/~anderson/
May 02 2004
prev sibling parent "Phill" <phill pacific.net.au> writes:
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c71ts1$2qr7$1 digitaldaemon.com...
 Phill wrote:

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c71eva$228o$1 digitaldaemon.com...


Scott Egan wrote:



(I found the use of just the varible names in the with block a bit
uncomfortable).

You've had to much VB.
Is that Victorian Bitter? or Visual Basic? Or both? Phill
Doesn't matter, they both make you look stupid. I apologise, I just couldn't resist.
true true :o))
May 02 2004
prev sibling next sibling parent Andy Friesen <andy ikagames.com> writes:
Matthew wrote:

 One of the things I've wanted in C++ for a long time is the ability to be able
to
 use scope in a similar vein to file-system location. In other words, I would
like
 to be able to have the following
 
     ::X - refers to X in global scope
     :X - refers to X in local scope
     ::::X - refers to X in next most local scope
 
 (only with better thought-out syntax)
 
 What I'm interested in are people's thoughts for whether this would be nice in
D,
 and suggestions for syntax.
I'm not sure I see a need for this. There should only ever be three scopes: module, class, and local, all of which can be accessed via distinct syntax. Further, D forbids locals from shadowing 'outer' locals in nested scopes. -- andy
May 01 2004
prev sibling parent reply "Phill" <phill pacific.net.au> writes:
"Matthew" <matthew.hat stlsoft.dot.org> wrote in message
news:c70phi$112a$1 digitaldaemon.com...
 One of the things I've wanted in C++ for a long time is the ability to be
able to
 use scope in a similar vein to file-system location. In other words, I
would like
 to be able to have the following

     ::X - refers to X in global scope
     :X - refers to X in local scope
     ::::X - refers to X in next most local scope

 (only with better thought-out syntax)

 What I'm interested in are people's thoughts for whether this would be
nice in D,
 and suggestions for syntax.
This would be a good idea for people that would use it no doubt. But I can say that I wouldnt use it. Reason: For me it is much faster and clearer to choose another name for the variable, and know which one I am reffering to, than to sit there and work out which "X" I am reffering to each time I look at the method or block of code. But as I said, this is for my case. Phill.
May 01 2004
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Phill wrote:

This would be a good idea for people that would
use it no doubt.
But I can say that I wouldnt use it.
Reason:
For me it is much faster and clearer to choose another name for the
variable, and know which one I am reffering to, than to sit there and work
out which "X" I am
reffering to each time I look at the method or block of code.

But as I said, this is for my case.

Phill.
  
I had some students in a VB graphics programming class I'm tutoring. It took me ages to convince them that the dot meant a different variable. In the end I just renamed it. -- -Anderson: http://badmama.com.au/~anderson/
May 01 2004
prev sibling parent "Jeroen van Bemmel" <someone somewhere.com> writes:
"Phill" <phill pacific.net.au> wrote in message 
news:c71std$2oug$1 digitaldaemon.com...
 "Matthew" <matthew.hat stlsoft.dot.org> wrote in message
 news:c70phi$112a$1 digitaldaemon.com...
 One of the things I've wanted in C++ for a long time is the ability to be
able to
 use scope in a similar vein to file-system location. In other words, I
would like
 to be able to have the following

     ::X - refers to X in global scope
     :X - refers to X in local scope
     ::::X - refers to X in next most local scope

 (only with better thought-out syntax)

 What I'm interested in are people's thoughts for whether this would be
nice in D,
 and suggestions for syntax.
This would be a good idea for people that would use it no doubt. But I can say that I wouldnt use it. Reason: For me it is much faster and clearer to choose another name for the variable, and know which one I am reffering to, than to sit there and work out which "X" I am reffering to each time I look at the method or block of code. But as I said, this is for my case. Phill.
I agree, this feature would encourage bad coding practices. Recommendation should be to use distinct names for variables as a rule, only in the most trivial and obviously non-conflicting (lazy programmer's) case should it be reluctantly endoursed. Perhaps the compiler should even emit warnings when variable names hide other variables in outer scopes. It should be able to work in a mode where this practice causes the program not to compile, such that companies can enforce it as a quality standard (I am thinking of a compiler switch here)
May 02 2004