www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The new '$'

reply "Craig Black" <cblack ara.com> writes:
I decided to start a new post because there was to much stuff on the old 
one.  Couldn't we just do something more generic than just for the length 
property, like importing all properties for the variable being indexed 
within the braces?  Essentially, within the braces would be local method 
scope for objects.

Feel free to shoot me down if its a bad idea.

-Craig 
Mar 10 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <d0q616$2qmt$1 digitaldaemon.com>, Craig Black says...
I decided to start a new post because there was to much stuff on the old 
one.  Couldn't we just do something more generic than just for the length 
property, like importing all properties for the variable being indexed 
within the braces?  Essentially, within the braces would be local method 
scope for objects.

Feel free to shoot me down if its a bad idea.

Sure. Not sure if this has been beaten to death but: Extend 'with()' to work with arrays, and scalars.
 int[] a,b;
 with(a) b = a[0..length];

It could only make things more consistent, IMO. - EricAnderton at yahoo
Mar 10 2005
next sibling parent reply "Craig Black" <cblack ara.com> writes:
I'm talking about an implicit "with", if you will.  No need to specify with. 
We are talking about shortcuts here aren't we?  I would rather say

b = a[0..a.length]

than

with(a) b = a[0..length]

but

b = a[0..length]

is even better.

So, perhaps the with should be implicit for every index operator.

-Craig 
Mar 10 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <d0q7ab$2rvk$1 digitaldaemon.com>, Craig Black says...
I'm talking about an implicit "with", if you will.  No need to specify with. 
We are talking about shortcuts here aren't we?  I would rather say

b = a[0..a.length]

than

with(a) b = a[0..length]

but

b = a[0..length]

is even better.

So, perhaps the with should be implicit for every index operator.

But that takes us back, full circle, to why the auto-length-variable in slice expressions causes problems with 'length' in outer scopes; hence '$'. At least 'with' is self-documenting, *and* its optional. If you're going to clobber the namespace, its best to be up-front about it. :) Now if you're pushing for an auto-with type of solution, then '$' should probably become a prefix for that scope ('$length' etc). The upshot is that its reasonably obvious in its use, while it can't hold a candle to '$' terseness. And '$' cannot be beat for its size, but it looks like a wart to die-hard C programmers. That brings me back to 'with()' since it doesn't compromise the way D looks. ::scratches head:: I can see why '$' will probably win out in the long run, simply because its short and has exactly *one* defintion in the language. But like using '?' instead of 'if', i'd like to have the option of using 'with()' when messing around with property scopes on arrays. - EricAnderton at yahoo
Mar 10 2005
next sibling parent "Craig Black" <cblack ara.com> writes:
 But that takes us back, full circle, to why the auto-length-variable in 
 slice
 expressions causes problems with 'length' in outer scopes; hence '$'.

In the case of length overshadowing a local variable, the compiler can generate a warning. IMO this solves the problem. Hoever, another issue is, as Walter pointed out, how do we handle classes that have the index operators overloaded. -Craig
Mar 10 2005
prev sibling parent jicman <jicman_member pathlink.com> writes:
First of all, I am not picking on anyone, but com'on folks!  We have worked this
theme to death.  We have raised it from the death.  Killed it again and revive
it with mouth 2 mouth resucitation.  I will start a bet right now: we are going
to end up with [0..length]  Who wants in?  Let's allow Walter to make this his
choice and let's argue something that we all can see and agree to be a great
change/add for d.  This is, perhaps, one of the bad results that freedom brings:
one could talk and argue about a subject for ever without ever coming into an
agreement.  It's one of the reasons that big companies are going out of the US
to get work done.  They just tell them what they want done and no one can say or
give an opinion...

By the way, this is just a thought... :-)

pragma says...
In article <d0q7ab$2rvk$1 digitaldaemon.com>, Craig Black says...
I'm talking about an implicit "with", if you will.  No need to specify with. 
We are talking about shortcuts here aren't we?  I would rather say

b = a[0..a.length]

than

with(a) b = a[0..length]

but

b = a[0..length]

is even better.

So, perhaps the with should be implicit for every index operator.

But that takes us back, full circle, to why the auto-length-variable in slice expressions causes problems with 'length' in outer scopes; hence '$'. At least 'with' is self-documenting, *and* its optional. If you're going to clobber the namespace, its best to be up-front about it. :) Now if you're pushing for an auto-with type of solution, then '$' should probably become a prefix for that scope ('$length' etc). The upshot is that its reasonably obvious in its use, while it can't hold a candle to '$' terseness. And '$' cannot be beat for its size, but it looks like a wart to die-hard C programmers. That brings me back to 'with()' since it doesn't compromise the way D looks. ::scratches head:: I can see why '$' will probably win out in the long run, simply because its short and has exactly *one* defintion in the language. But like using '?' instead of 'if', i'd like to have the option of using 'with()' when messing around with property scopes on arrays. - EricAnderton at yahoo

Mar 10 2005
prev sibling parent reply xs0 <xs0 xs0.com> writes:
pragma wrote:
 In article <d0q616$2qmt$1 digitaldaemon.com>, Craig Black says...
 
I decided to start a new post because there was to much stuff on the old 
one.  Couldn't we just do something more generic than just for the length 
property, like importing all properties for the variable being indexed 
within the braces?  Essentially, within the braces would be local method 
scope for objects.

Feel free to shoot me down if its a bad idea.

Sure. Not sure if this has been beaten to death but: Extend 'with()' to work with arrays, and scalars.
int[] a,b;
with(a) b = a[0..length];

It could only make things more consistent, IMO. - EricAnderton at yahoo

but with() can't be used for multi-dimensional arrays, afaik.. how would you write int[][][] a; a[$-1][$-1][$-1]=5; ?
Mar 11 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <d0rrvm$1kk8$2 digitaldaemon.com>, xs0 says...
pragma wrote:
 In article <d0q616$2qmt$1 digitaldaemon.com>, Craig Black says...
 
I decided to start a new post because there was to much stuff on the old 
one.  Couldn't we just do something more generic than just for the length 
property, like importing all properties for the variable being indexed 
within the braces?  Essentially, within the braces would be local method 
scope for objects.

Feel free to shoot me down if its a bad idea.

Sure. Not sure if this has been beaten to death but: Extend 'with()' to work with arrays, and scalars.
int[] a,b;
with(a) b = a[0..length];

It could only make things more consistent, IMO. - EricAnderton at yahoo

but with() can't be used for multi-dimensional arrays, afaik.. how would you write int[][][] a; a[$-1][$-1][$-1]=5; ?

Agreed. "with()" doesn't cover all the bases, so it's not an all-round solution. I still feel that "with()" is broken, and should be extended to work with arrays anyway. I've also posted on the pros/cons of this and other methods... the only conclusion I can come to is that '$' seems to have *most* of the properties that folks want. Your example supports that. :) - EricAnderton at yahoo
Mar 11 2005
parent David Medlock <amedlock nospam.org> writes:
pragma wrote:
 In article <d0rrvm$1kk8$2 digitaldaemon.com>, xs0 says...
 
pragma wrote:

In article <d0q616$2qmt$1 digitaldaemon.com>, Craig Black says...


I decided to start a new post because there was to much stuff on the old 
one.  Couldn't we just do something more generic than just for the length 
property, like importing all properties for the variable being indexed 
within the braces?  Essentially, within the braces would be local method 
scope for objects.

Feel free to shoot me down if its a bad idea.

Sure. Not sure if this has been beaten to death but: Extend 'with()' to work with arrays, and scalars.
int[] a,b;
with(a) b = a[0..length];

It could only make things more consistent, IMO. - EricAnderton at yahoo

but with() can't be used for multi-dimensional arrays, afaik.. how would you write int[][][] a; a[$-1][$-1][$-1]=5; ?

Agreed. "with()" doesn't cover all the bases, so it's not an all-round solution. I still feel that "with()" is broken, and should be extended to work with arrays anyway. I've also posted on the pros/cons of this and other methods... the only conclusion I can come to is that '$' seems to have *most* of the properties that folks want. Your example supports that. :) - EricAnderton at yahoo

I would argue that for dimensions greater than 2 you should be using access routines (in which clarity is key) and should be using the array.length syntax. I would bet that the ratio of single dimension to multi dimension use is like 100:1 -David
Mar 11 2005