Jump to page: 1 25  
Page
Thread overview
The new "$" has an inconsistent behavior
Mar 09, 2005
David L. Davis
Mar 09, 2005
David Medlock
Re: The new
Mar 09, 2005
pragma
Mar 09, 2005
David Medlock
Mar 09, 2005
Kris
Mar 09, 2005
Kris
Mar 09, 2005
Matthew
Another way
Mar 10, 2005
Walter
Mar 10, 2005
David Medlock
Mar 10, 2005
Walter
with() is broken (was: Re: Another way)
Mar 10, 2005
pragma
Mar 10, 2005
Matthew
Mar 10, 2005
Benji Smith
Mar 13, 2005
Regan Heath
Mar 14, 2005
Regan Heath
Mar 10, 2005
xs0
Mar 10, 2005
Matthew
Mar 10, 2005
David Medlock
Mar 10, 2005
Matthew
Mar 10, 2005
Derek Parnell
Mar 09, 2005
Ben Hinkle
Mar 09, 2005
Matthew
Mar 10, 2005
David Medlock
Mar 10, 2005
Matthew
Mar 10, 2005
David Medlock
Mar 10, 2005
Matthew
Mar 10, 2005
David Medlock
Mar 10, 2005
Matthew
Mar 10, 2005
John C
Re: The new
Mar 10, 2005
Ben Hinkle
Mar 09, 2005
Lionello Lunesu
Mar 09, 2005
Derek Parnell
Re: The new
Mar 09, 2005
Kris
Mar 10, 2005
Derek Parnell
Re: The new
Mar 09, 2005
Dave
Re: The new '$' has an inconsistent behavior
Mar 09, 2005
David L. Davis
Mar 10, 2005
Stewart Gordon
Re: The new '$' has an inconsistent behavior
Mar 10, 2005
David L. Davis
Mar 10, 2005
Stewart Gordon
Mar 10, 2005
David L. Davis
Mar 10, 2005
Stewart Gordon
Finally: The new '$' has an NO inconsistent behavior
Mar 10, 2005
MicroWizard
Apr 10, 2005
Shammah
March 09, 2005
The "$" appears to have an inconsistent behavior as seen in the test program below, and it really should be nailed down before anyone uses it in their D programs…once of course, it's approved that it will be used to stand for array.length when it's between the array brackets ([ $ ]). So, should "$" stand for "length - 1" or "length?" Myself I'd like for it to stand for "length - 1," because it saves me some extra typing by removing the need for "- 1."

Tho I still think using the "$" for this is still a waste of a good symbol which could be put to better use on something else. If it's approved by the group, then I will use it a lot!! :)

# // test2.d
# private import std.stdio;
#
# int main()
# {
#     char[] sTest = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
#
#     // Here "$" does "length - 1"
#     writefln( "\"" ~ sTest[ 0 .. $ ] ~ "\"" );
#
#     // Here "$" does "length - 1," not "length - 2"
#     writefln( "\"", sTest[ $ - 1 ], "\"" );
#
#     // Here "$" won't compile as is, and gets the following errors:
#     // test2.d(8): incompatible types for (("\"") ~ (cast(int)(sTest[cast
#                    (int)(__dollar - 1)]))): 'char[]' and 'int'
#     // test2.d(8): Can only concatenate arrays, not (char[] ~ int)
#     // test2.d(8): incompatible types for (("\"" ~ cast(int)(sTest[cast(int)
#                    (__dollar - 1)])) ~ ("\"")): 'int' and 'char[]'
#     // test2.d(8): Can only concatenate arrays, not (int ~ char[])
#     // test2.d(9): incompatible types for (("\"") ~ (cast(int)(sTest[cast
#                    (int)(__dollar)]))): 'char[]' and 'int'
#     // test2.d(9): Can only concatenate arrays, not (char[] ~ int)
#     // test2.d(9): incompatible types for (("\"" ~ cast(int)(sTest[cast(int)
#                    (__dollar)])) ~ ("\"")): 'int' and 'char[]'
#     // test2.d(9): Can only concatenate arrays, not (int ~ char[])
#     //writefln( "\"" ~ sTest[ $ - 1 ] ~ "\"" );
#
#     // Here it compiles both, but "$" gives:
#     // Error: ArrayBoundsError test2(24)
#     //writefln( "\"", sTest[ $ ], "\"" );
#
#     // Error: ArrayBoundsError test2(24)
#     //writefln( sTest[ $ ] );
#
#     // "$" compiles, and gives "Z", the same as "length - 1"
#     writefln( "\"", sTest[ $ - 1 ], "\"" );
#
#     return 0;
# }

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
March 09, 2005
<snip>

I think the $ is a bad choice.

What does a dollar sign have to do with length of an array?
We use '.' and [] because C/C++ uses them.
What is the precedence for '$'?
It lowers readability and doesn't add much at all to the language.

This similar to some choices made durings C++'s lifetime(C too! see trigraphs) which makes for cluttered code, lots of special syntaxes to rememeber, and general inconsistency.  Not this one decision, but add them all up and you get a mess.

All my opinion, of course.
-David

March 09, 2005
In article <d0niiq$31d5$1@digitaldaemon.com>, David Medlock says...
>
><snip>
>
>I think the $ is a bad choice.
>
>What does a dollar sign have to do with length of an array?
>We use '.' and [] because C/C++ uses them.
>What is the precedence for '$'?
>It lowers readability and doesn't add much at all to the language.

I think the use of '$' can be traced back to regular expressions, where it represents 'end of line'.  It means almost the same thing in this context ('end of string/array'), so I wouldn't think it a bad token to use per-se.

It *does* take some getting used to though.

- EricAnderton at yahoo
March 09, 2005
pragma wrote:
> In article <d0niiq$31d5$1@digitaldaemon.com>, David Medlock says...
> 
>><snip>
>>
>>I think the $ is a bad choice.
>>
>>What does a dollar sign have to do with length of an array?
>>We use '.' and [] because C/C++ uses them.
>>What is the precedence for '$'?
>>It lowers readability and doesn't add much at all to the language.
> 
> 
> I think the use of '$' can be traced back to regular expressions, where it
> represents 'end of line'.  It means almost the same thing in this context ('end
> of string/array'), so I wouldn't think it a bad token to use per-se.
> 
> It *does* take some getting used to though.
> 
> - EricAnderton at yahoo


I hear you, however using regular expressions( which are neither regular nor (readably)expressive ) as a basis for new syntax is still up for debate...

Perl 6 is tossing the old regex inconsistencies.
Regexes are meant to be space conscious at the cost of readability, whereas code really shouldn't be.

It should be as readable as possible, since maintenance is something like 70+%(?) of the cost of software.

In doing my DManager stuff, I looked through some old Delphi code to brush up and I was amazed at how readable it is.  I am not saying Walter should emulate Pascal, but its readability a worthy attribute.

Another $0.02 from
-David
March 09, 2005
> #     // Here "$" does "length - 1"
> #     writefln( "\"" ~ sTest[ 0 .. $ ] ~ "\"" );

I think that's just the way the slicing operator works: from 0 'up to but not including' length. It's the same if you write it out using 'the old syntax': array[0..length] will get you elements [0] till [length-1].

http://www.digitalmars.com/d/arrays.html#slicing

> #     // Here "$" does "length - 1," not "length - 2"
> #     writefln( "\"", sTest[ $ - 1 ], "\"" );

I haven't tested it, but if it were 'length' it should have printed the last element in the array?

L.


March 09, 2005
On Thu, 10 Mar 2005 05:56:21 +1100, David L. Davis <SpottedTiger@yahoo.com> wrote:

> The "$" appears to have an inconsistent behavior as seen in the test program
> below, and it really should be nailed down before anyone uses it in their D
> programs…once of course, it's approved that it will be used to stand for
> array.length when it's between the array brackets ([ $ ]). So, should "$" stand
> for "length - 1" or "length?" Myself I'd like for it to stand for "length - 1,"
> because it saves me some extra typing by removing the need for "- 1."

Ummm...no it is working exactly as if you had have typed "sTest.length" instead of "$".

You have to remember the weird way that slice references work. Namely that a slice of [X .. Y] selects all the elements from X up to but not including Y. And that references are zero-based. Thus array[array.length] always references the non-existant element just after the last one. And array[0 .. array.length] references element 0 through to, but not including, the non-existant one just beyond the end.

Weird I know, but Walter has chosen.


-- 
Derek
March 09, 2005
"David Medlock" <amedlock@nospam.org> wrote in message news:d0niiq$31d5$1@digitaldaemon.com...
> <snip>
>
> I think the $ is a bad choice.
>
> What does a dollar sign have to do with length of an array?
> We use '.' and [] because C/C++ uses them.
> What is the precedence for '$'?
> It lowers readability and doesn't add much at all to the language.
>
> This similar to some choices made durings C++'s lifetime(C too! see trigraphs) which makes for cluttered code, lots of special syntaxes to rememeber, and general inconsistency.  Not this one decision, but add them all up and you get a mess.
>
> All my opinion, of course.
> -David
>

I agree. Using an identifier is something people recognize and can understand. Seeing $ will confuse people and think there is more magic in there than there is.  When I see $ used for this (or even when I imagine $length, $arguments, etc) I can't relate it to any other C-like language construct that I know. It actually looks to me like a preprocessor hook of some kind or shell-like environment variable expansion.


March 09, 2005
In article <op.sndzksf7nxgd8b@umalgas>, Derek Parnell says...
>
>On Thu, 10 Mar 2005 05:56:21 +1100, David L. Davis <SpottedTiger@yahoo.com> wrote:
>
>> The "$" appears to have an inconsistent behavior as seen in the test
>> program
>> below, and it really should be nailed down before anyone uses it in
>> their D
>> programs…once of course, it's approved that it will be used to stand for
>> array.length when it's between the array brackets ([ $ ]). So, should
>> "$" stand
>> for "length - 1" or "length?" Myself I'd like for it to stand for
>> "length - 1,"
>> because it saves me some extra typing by removing the need for "- 1."
>
>Ummm...no it is working exactly as if you had have typed "sTest.length" instead of "$".
>
>You have to remember the weird way that slice references work. Namely that a slice of [X .. Y] selects all the elements from X up to but not including Y. And that references are zero-based. Thus array[array.length] always references the non-existant element just after the last one. And array[0 .. array.length] references element 0 through to, but not including, the non-existant one just beyond the end.
>
>Weird I know, but Walter has chosen.


FWIW; what Walter has done is a compromise over what is sometimes called "end-point paranoia" ~ most often witnessed in graphics libs, but relevant here also (google that phrase to find some QuickDraw links).

When writing the Mango HTTP server, I don't recall one time where I had to compensate for such things; and there's a whole lot of slicing going on in there :-)

I know it seems a bit wacky that part of slicing is 'n' and another part is 'n-1', but I've found it to work well in practice.


March 09, 2005
In article <op.sndzksf7nxgd8b@umalgas>, Derek Parnell says...
>
>On Thu, 10 Mar 2005 05:56:21 +1100, David L. Davis <SpottedTiger@yahoo.com> wrote:
>
>> The "$" appears to have an inconsistent behavior as seen in the test
>> program
>> below, and it really should be nailed down before anyone uses it in
>> their D
>> programs…once of course, it's approved that it will be used to stand for
>> array.length when it's between the array brackets ([ $ ]). So, should
>> "$" stand
>> for "length - 1" or "length?" Myself I'd like for it to stand for
>> "length - 1,"
>> because it saves me some extra typing by removing the need for "- 1."
>
>Ummm...no it is working exactly as if you had have typed "sTest.length" instead of "$".
>
>You have to remember the weird way that slice references work. Namely that a slice of [X .. Y] selects all the elements from X up to but not including Y. And that references are zero-based. Thus array[array.length] always references the non-existant element just after the last one. And array[0 .. array.length] references element 0 through to, but not including, the non-existant one just beyond the end.
>
>Weird I know, but Walter has chosen.
>

It works that way to be consistent with 0 based array elements. Otherwise, you'd be having to do things like this all the time:

arr[X .. arr.length - 1]; /* or */ arr[X .. $ - 1] /* etc.. */

I just think of it as for(int i = X; i < Y; i++) { /*...*/ }

>
>-- 
>Derek


March 09, 2005
> I think the use of '$' can be traced back to regular expressions,

Of course, to someone who has no idea what regex syntax even is, it just seems stupid.  Especially coming from a BASIC background, in which $ is the "string variable" suffix.

> It *does* take some getting used to though.

I really hate how length has been deprecated.  I really don't like the $ one bit.


« First   ‹ Prev
1 2 3 4 5