www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - "#" symbol

reply Tomás Rossi <Tomás_member pathlink.com> writes:
Sorry for my D syntax ignorance but, is the "#" symbol used for anything in D?
I ask cause I was reading the discussion about de "$" vs. "length". "#" is much
more natural for the purpose (speaking of the cardinal or length of something).
Also the "$" symbol feels like a waste in using it as length.

Regards

Tom
Oct 23 2005
next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Tomás Rossi wrote:
 Sorry for my D syntax ignorance but, is the "#" symbol used for anything in D?
 I ask cause I was reading the discussion about de "$" vs. "length". "#" is much
 more natural for the purpose (speaking of the cardinal or length of something).
 Also the "$" symbol feels like a waste in using it as length.
 
 Regards
 
 Tom

special token sequence: # line <number> [<filename>] or actually: SpecialTokenSequence # line Integer EndOfLine # line Integer Filespec EndOfLine Filespec " Characters "
Oct 23 2005
parent reply Tomás Rossi <Tomás_member pathlink.com> writes:
In article <djhkm1$303g$1 digitaldaemon.com>, Hasan Aljudy says...
Tomás Rossi wrote:
 Sorry for my D syntax ignorance but, is the "#" symbol used for anything in D?
 I ask cause I was reading the discussion about de "$" vs. "length". "#" is much
 more natural for the purpose (speaking of the cardinal or length of something).
 Also the "$" symbol feels like a waste in using it as length.
 
 Regards
 
 Tom

special token sequence: # line <number> [<filename>] or actually: SpecialTokenSequence # line Integer EndOfLine # line Integer Filespec EndOfLine Filespec " Characters "

Oh, ok, though I can't see in which cases would anyone use that special token sequence anyway. Seem's like a preprocessor command to me, like the ones Walter took away for many reasons. Another waste of nice symbols :) Tom
Oct 24 2005
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Tomás Rossi wrote:
 In article <djhkm1$303g$1 digitaldaemon.com>, Hasan Aljudy says...
 
Tomás Rossi wrote:

Sorry for my D syntax ignorance but, is the "#" symbol used for anything in D?
I ask cause I was reading the discussion about de "$" vs. "length". "#" is much
more natural for the purpose (speaking of the cardinal or length of something).
Also the "$" symbol feels like a waste in using it as length.

Regards

Tom

http://www.digitalmars.com/d/lex.html special token sequence: # line <number> [<filename>] or actually: SpecialTokenSequence # line Integer EndOfLine # line Integer Filespec EndOfLine Filespec " Characters "

Oh, ok, though I can't see in which cases would anyone use that special token sequence anyway. Seem's like a preprocessor command to me, like the ones Walter took away for many reasons. Another waste of nice symbols :) Tom

I personally don't know why would anyone need to use it .. I haven't seen any discussion on it in this NG!
Oct 24 2005
parent reply pragma <pragma_member pathlink.com> writes:
In article <djimq6$rjp$1 digitaldaemon.com>, Hasan Aljudy says...
Tomás Rossi wrote:
 In article <djhkm1$303g$1 digitaldaemon.com>, Hasan Aljudy says...
 
Tomás Rossi wrote:

Sorry for my D syntax ignorance but, is the "#" symbol used for anything in D?
I ask cause I was reading the discussion about de "$" vs. "length". "#" is much
more natural for the purpose (speaking of the cardinal or length of something).
Also the "$" symbol feels like a waste in using it as length.

Regards

Tom

http://www.digitalmars.com/d/lex.html special token sequence: # line <number> [<filename>] or actually: SpecialTokenSequence # line Integer EndOfLine # line Integer Filespec EndOfLine Filespec " Characters "

Oh, ok, though I can't see in which cases would anyone use that special token sequence anyway. Seem's like a preprocessor command to me, like the ones Walter took away for many reasons. Another waste of nice symbols :) Tom

I personally don't know why would anyone need to use it .. I haven't seen any discussion on it in this NG!

AFAIK, this feature is intended for preprocessors and code-generators, and is never really used during 'manual' programming. Actually, I'm using it quite heavily for DSP. For example, given the following HTML: <b>hello world</b> <dsp:code>uint a = ;</dsp:code> DSP generates something like the following: // output file: helloworld.d /** DSP runtime stuff and additional code would appear here **/ #line 1 "helloworld.dsp" response.put("<b>hello world</b>"); #line 2 "helloworld.dsp" uint a = ; This feature makes debugging actually possible, as the line numbers reported by DMD will correspond to the line numbers in the actual source. The syntax error on line #2 will be reported by DMD as an error on line #2 in "helloworld.dsp" instead of line #6 in "helloworld.d". Use of #line makes such a virtual line number mapping possible, without having to resort to parsing the compiler's output while maintaining some sort of line number translation table (icky). Now, as to wether or not its a waste of a symbol, #line is the only directive of its kind supported by the language, so arguably its not a good use of '#'. Honestly, I wouldn't mind if #line were changed into "pragma(line,lineno,"filename")" instead; if anything it would appear to be less of language wart in that respect. - EricAnderton at yahoo
Oct 24 2005
parent "Walter Bright" <newshound digitalmars.com> writes:
"pragma" <pragma_member pathlink.com> wrote in message
news:djipt1$uag$1 digitaldaemon.com...
 AFAIK, this feature is intended for preprocessors and code-generators, and

 never really used during 'manual' programming.

That's right.
 Actually, I'm using it quite heavily for DSP.  For example, given the

 HTML:

 <b>hello world</b>
 <dsp:code>uint a = ;</dsp:code>

 DSP generates something like the following:

 // output file: helloworld.d
 /** DSP runtime stuff and additional code would appear here **/
 #line 1 "helloworld.dsp"
 response.put("<b>hello world</b>");
 #line 2 "helloworld.dsp"
 uint a = ;

 This feature makes debugging actually possible, as the line numbers

 DMD will correspond to the line numbers in the actual source.  The syntax

 on line #2 will be reported by DMD as an error on line #2 in

 instead of line #6 in "helloworld.d".

 Use of #line makes such a virtual line number mapping possible, without

 to resort to parsing the compiler's output while maintaining some sort of

 number translation table (icky).

Yes.
 Now, as to wether or not its a waste of a symbol, #line is the only

 its kind supported by the language, so arguably its not a good use of '#'.
 Honestly, I wouldn't mind if #line were changed into
 "pragma(line,lineno,"filename")" instead; if anything it would appear to

 of language wart in that respect.

The idea was so one could use the C preprocessor without modification.
Oct 24 2005
prev sibling parent reply Dan <Dan_member pathlink.com> writes:
Curious why you folk are insistent on putting obscure unreadable
characters into a compiled language.  What is the value?

arr.length says it loud and clear.

arr.# says?  pound?  number?

arr.$ says?  money!  a whole array of it!

It doesn't make the binary smaller.  

It doesn't make the code easier to read.

It makes it about three keystrokes easier (shift down, pound, shift up), but if
you really are in that much of a hurry maybe copy and paste it.  Just double
click on length and Ctrl C, Ctrl V.

Unless you care to inform me why it matters?
Oct 25 2005
next sibling parent Daniel Horn <hellcatv hotmail.com> writes:
People are complaining about the language construct that automatically 
binds a local variable length to the array's length... hiding class vars 
and local vars
i.e.

int length=0;
int array [10];
return array[length];
will segfault...because the code is translated into

int length=0;
int array [10];
{
   int length=array.length;
   return array[length];
}

so people suggested using characters that you could NOT name an 
identifier to prevent the above mistake...
and $ was one of them--anything is better than the above mistake that's 
currently in the compiler--including your solution of naming array each 
time.


Dan wrote:
 Curious why you folk are insistent on putting obscure unreadable
 characters into a compiled language.  What is the value?
 
 arr.length says it loud and clear.
 
 arr.# says?  pound?  number?
 
 arr.$ says?  money!  a whole array of it!
 
 It doesn't make the binary smaller.  
 
 It doesn't make the code easier to read.
 
 It makes it about three keystrokes easier (shift down, pound, shift up), but if
 you really are in that much of a hurry maybe copy and paste it.  Just double
 click on length and Ctrl C, Ctrl V.
 
 Unless you care to inform me why it matters?
 
 

Oct 25 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 25 Oct 2005 23:47:02 +0000 (UTC), Dan wrote:

 Curious why you folk are insistent on putting obscure unreadable
 characters into a compiled language.  What is the value?
 
 arr.length says it loud and clear.

Yes it does.
 arr.# says?  pound?  number?
 
 arr.$ says?  money!  a whole array of it!

No one is suggesting these syntaxes, Dan. What is being suggested is that '$' or '#' is a shorthand for 'arr.length' in the context of a slice expression. It is not shorthand for the word 'length' but the combined array name *and* the length property.
 It doesn't make the binary smaller.  

No, but so what? We are talking about the D language which is a human-readable language.
 It doesn't make the code easier to read.

This is the point of most dispute. I suspect that we need some empirical evidence to resolve it.
 It makes it about three keystrokes easier (shift down, pound, shift up), but if
 you really are in that much of a hurry maybe copy and paste it.  Just double
 click on length and Ctrl C, Ctrl V.

SomeClass.Selected_Items[SomeClass.Selected_Items.length-1] &= AnotherClass.User_Choice[AnotherClass.User_Choice.length-2 .. AnotherClass.User_Choice.length-1]; SomeClass.Selected_Items[$-1] &= AnotherClass.User_Choice[$-2 .. $-1];
 Unless you care to inform me why it matters?

I still think that code legibility is enhanced by using a small symbol to represent the concept of 'the current array's length'. -- Derek (skype: derek.j.parnell) Melbourne, Australia 26/10/2005 9:52:57 AM
Oct 25 2005
next sibling parent reply Dan <Dan_member pathlink.com> writes:
Okay, so the problem is the scope issue, and when "array" is a 63 character long
namespace.  Both are valid points.

Hmm...

To prevent the scope issues we might try:
a) make array/struct etc. properties only accessible using the myArray.length
instead of just length.
b) make it an identifier
c) leave it the way it is and document it

To cope with the long namespace issue and slicing:
a) use a variable.
b) use an alias.
c) Maybe the MyObject.  (and later) .property  or .method() might not be a bad
idea?

Another problem you suddenly run into when using $ for "this array" is correctly
and consistently making it obvious what "this array" is.  As you mentioned in
your own example, we had two arrays with messy names.  How do you switch the $
to mean one and then the other?
Oct 25 2005
parent Derek Parnell <derek psych.ward> writes:
On Wed, 26 Oct 2005 03:24:24 +0000 (UTC), Dan wrote:


[snip]

 
 Another problem you suddenly run into when using $ for "this array" is
correctly
 and consistently making it obvious what "this array" is.  As you mentioned in
 your own example, we had two arrays with messy names.  How do you switch the $
 to mean one and then the other?

You don't. It always refers to the array being sliced. -- Derek (skype: derek.j.parnell) Melbourne, Australia 26/10/2005 1:57:26 PM
Oct 25 2005
prev sibling next sibling parent Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Derek Parnell wrote:
It makes it about three keystrokes easier (shift down, pound, shift up), but if
you really are in that much of a hurry maybe copy and paste it.  Just double
click on length and Ctrl C, Ctrl V.

SomeClass.Selected_Items[SomeClass.Selected_Items.length-1] &= AnotherClass.User_Choice[AnotherClass.User_Choice.length-2 .. AnotherClass.User_Choice.length-1]; SomeClass.Selected_Items[$-1] &= AnotherClass.User_Choice[$-2 .. $-1];

What you speak here is a particular case of a more general issue, which is having several repeated names in a piece of code. For instance in: SomeClass.Selected_Items.SomeMethod( SomeClass.Selected_Items.member, AnotherClass.User_Choice.member + AnotherClass.User_Choice.member*2); we have a similar problem. The solution you have (whether with "$" or "length") is particular for arrays only, and it works only for accessing the length of the indexed array, not of other arrays. Why not use the general solution to this issue, that is, use alias and/or intermediate objects to refactor code, as appropriate for the semantics of the code: auto selItems = SomeClass.Selected_Items; auto member = AnotherClass.User_Choice.member; selItems.SomeMethod( selItems.member, member + member*2); Is it worth it, to have a keyword that works only in a specific case with arrays? I'm not saying yet that it isn't, but I think I'm more inclined to writing verbose code (with or without intermediate objects, depending) which only sometimes, perhaps not very often, will be annoyingly bigger than a "$" keyword version. Also, note that the existence of IDE code auto-completion will soften most of the burden of writing the verbose way. -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Oct 26 2005
prev sibling parent reply "Kris" <fu bar.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message
 SomeClass.Selected_Items[SomeClass.Selected_Items.length-1] &=
        AnotherClass.User_Choice[AnotherClass.User_Choice.length-2 ..
                                 AnotherClass.User_Choice.length-1];

 SomeClass.Selected_Items[$-1] &= AnotherClass.User_Choice[$-2 .. $-1];

 Unless you care to inform me why it matters?

I still think that code legibility is enhanced by using a small symbol to represent the concept of 'the current array's length'.

True. Yet, is there really such an issue with the following alternate sugar? The benefits are notable: SomeClass.Selected_Items [$len-1] &= AnotherClass.User_Choice [$len-2 .. $len-1];
Oct 30 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Sun, 30 Oct 2005 13:08:46 -0800, Kris wrote:

 "Derek Parnell" <derek psych.ward> wrote in message
 SomeClass.Selected_Items[SomeClass.Selected_Items.length-1] &=
        AnotherClass.User_Choice[AnotherClass.User_Choice.length-2 ..
                                 AnotherClass.User_Choice.length-1];

 SomeClass.Selected_Items[$-1] &= AnotherClass.User_Choice[$-2 .. $-1];

 Unless you care to inform me why it matters?

I still think that code legibility is enhanced by using a small symbol to represent the concept of 'the current array's length'.

True. Yet, is there really such an issue with the following alternate sugar? The benefits are notable: SomeClass.Selected_Items [$len-1] &= AnotherClass.User_Choice [$len-2 .. $len-1];

No. I said "small symbol" and "$len" looks small to me. -- Derek (skype: derek.j.parnell) Melbourne, Australia 31/10/2005 9:30:17 AM
Oct 30 2005
parent "Kris" <fu bar.com> writes:
"Derek Parnell" <derek psych.ward> wrote ...
 On Sun, 30 Oct 2005 13:08:46 -0800, Kris wrote:
 I still think that code legibility is enhanced by using a small symbol 
 to
 represent the concept of 'the current array's length'.

True. Yet, is there really such an issue with the following alternate sugar? The benefits are notable: SomeClass.Selected_Items [$len-1] &= AnotherClass.User_Choice [$len-2 .. $len-1];

No. I said "small symbol" and "$len" looks small to me.

Me too :-)
Oct 30 2005