Topic: variable name beginning with number


Author: ccg@freemail.c3.hu (Andras Erdei)
Date: 1999/04/21
Raw View
On 07 Apr 99 15:13:57 GMT, sbnaran@bardeen.ceg.uiuc.edu (Siemel
Naran) wrote:

>In the next revision of C++, it would be nice if we could have
>variables beginning with numbers.  Eg,
>   class 2dPhonon { ... };
>Hope everyone agrees!

No. Because things like 0xDeadBeef, increasing number
of typos (i don't know if this is a real concern) and slower
compilation.

Use dd_phonon. (I was working for company making an
architectural CAD program and we used this style -- i
think its pretty readable: u are most likely use only d_,
dd_ and ddd_.)

off-topic:

I *love* 1_000_000. Also i don't understand why there is
no 0b11110000.

Br

  Erdei Andras
  ccg@freemail.c3.hu
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "AllanW {formerly AllanW@my-dejanews.com}" <allan_w@my-dejanews.com>
Date: 1999/04/13
Raw View
In article <p7NO2.362$kM2.44452@burlma1-snr2>,
  Barry Margolin <barmar@bbnplanet.com> wrote:
> The general problem with this is that it makes it harder to distinguish
> identifiers from numbers.  2ea would be an identifier, while 2e2 would be a
> number.  2d would be an identifier, but 2l would be a number.
>
> Another issue is that it makes it harder for the language to evolve.  For
> instance, if 2i is currently treated as an identifier, it would be harder
> for a future version of the standard to specify this as the syntax for an
> imaginary number.

These two problems are enough to make the idea very unappealing, even if
we can "solve" them to some degree.

> Common Lisp tackled this by defining a syntax called "potential numbers".
> Any token that's not a potential number is guaranteed to be interpreted as
> a symbol in all future versions of the CL standard.  A potential number
> satisfies the following requirements:
[...]
> So, 2dPhonon is a potential number (in fact, it's an integer) in base 36 or
> higher, but a symbol in lower bases.

I think you mean base 37 or higher. The letter 'P' would have value
36. Just as base 10 can have digits 0-9, base 37 can have digits with
values 0-36.

----
Allan_W@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "AllanW {formerly AllanW@my-dejanews.com}" <allan_w@my-dejanews.com>
Date: 1999/04/13
Raw View
In article <370D493F.B8ADEB87@technologist.com>,
  David R Tribble <dtribble@technologist.com> wrote:
> What's wrong with 'TwoDPhonon', or for that matter, 'Phonon2d'?
> Of course, you might want to call it '_2dPhonon', but this
> breaks the rule about leading underscores for reserved names.
> How about 'a2dPhonon' or 'A2dPhonon'?  Surely you can come up
> with a reasonable sounding name, even within the lexical
> constraints of the language.

Agreed.

In the earliest incarnations of the language BASIC, variable names
had to be a single letter, or else a single letter followed by a
single digit. Yes, this did create hardships, but not as much as
you might think. (The *REAL* hardship was that every variable was
global, so a loop in a subroutine required a cross-reference check
for an unused variable name.) We tended to give adjacent names to
related data items, so that what would now be called an "Employee
Object" might have been called "The E-Series," along these lines:

    Rem *** E *** Employee information
    Rem E$  - Employee name
    Rem E1$ = Employee Address 1
    Rem E2$ = Employee Address 2
    Rem E3$ = Employee City
    Rem E4$ = Employee State
    Rem E5$ = Employee Zip code
    Rem (Note: we weren't international-conscious back then)
    Rem ... (Note that E4 and E4$ are distinct)
    Rem E9  = Employee Salary Year-To-Date

This is where I learned good internal documentation habits. All my
production code has a comment explaining the purpose of almost
every variable except local loop indexes, even if the variable
name already makes this clear.

> Either way, you'll find that allowing identifiers to start with
> a digit creates too many lexical problems, as other posts in this
> thread have pointed out.  (E.g., 2D, 2U, 0xG, 0xF, 2eA, 2e0, ...)

Agreed.

> A related topic (which was discussed a few months ago on comp.std.c)

Not really closely related, is it? One thread discusses legal names,
the other discusses legal numeric constants.

> was the possibility of allowing underscores within numbers to allow
> them to be more readable, e.g., 1_000_000.  The best suggestions
> were the following rules for numbers:
>  1. require a leading digit,
>  2. allow embedded underscores,
>  3. disallow trailing underscores,
>  4. disallow adjacent underscores (i.e., there must be digits
>     both to the left and the right of each underscore).
>
> Thus 1_000_000 and 0xFABC_DEF0 are valid, but 1__000_000 and
> 0_x_FABC_DEF0_ are not.  Such a proposal would not break any
> existing code, nor would it unduly burden real lexical analyzers.

I like this too. This proposal is careful not to change the semantics
of any valid code today, and at the same time it's difficult for me
to imagine anyone being particularly confused about what the meaning
is. That is,
    x = 1_000_000;
can't exactly look like it's reading from a global variable instead
of assigning with a numeric constant.

...Or, given the original context of this thread, could it?

----
Allan_W@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James.Kanze@dresdner-bank.com
Date: 1999/04/13
Raw View
In article <slrn7gi85n.chv.sbnaran@bardeen.ceg.uiuc.edu>,
  sbnaran@KILL.uiuc.edu wrote:
> In the next revision of C++, it would be nice if we could have
> variables beginning with numbers.  Eg,
>    class 2dPhonon { ... };
> Hope everyone agrees!

Totally.  I've always wanted to have a variable called 1e10.  Let's
allow [+-.], while we're at it, so we can have some real fun.

--
James Kanze                         mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orient   e objet/
                        Beratung in objekt orientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany  Tel. +49 (069) 63 19 86 27

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/04/09
Raw View
Siemel Naran wrote:
>
> Gabriel Dos_Reis <gdosreis@korrigan.inria.fr> wrote:
> >sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran) writes:
>
> >| In the next revision of C++, it would be nice if we could have
> >| variables beginning with numbers.  Eg,
> >|    class 2dPhonon { ... };
> >| Hope everyone agrees!
> >
> >I can hardly agree with you, with so few argumentation.
>
> What more is there to say?  When speaking to my friends I say "two
> dimensional phonon".  Hence, I like to call the class "2dPhonon".
> But because names can't begin with numbers, I have to call the class
> "Phonon2d".  Which isn't that bad, although I do prefer the more
> intuitive sounding "2dPhonon".

What's wrong with 'TwoDPhonon', or for that matter, 'Phonon2d'?
Of course, you might want to call it '_2dPhonon', but this
breaks the rule about leading underscores for reserved names.
How about 'a2dPhonon' or 'A2dPhonon'?  Surely you can come up
with a reasonable sounding name, even within the lexical
constraints of the language.

Either way, you'll find that allowing identifiers to start with
a digit creates too many lexical problems, as other posts in this
thread have pointed out.  (E.g., 2D, 2U, 0xG, 0xF, 2eA, 2e0, ...)

A related topic (which was discussed a few months ago on comp.std.c)
was the possibility of allowing underscores within numbers to allow
them to be more readable, e.g., 1_000_000.  The best suggestions
were the following rules for numbers:
 1. require a leading digit,
 2. allow embedded underscores,
 3. disallow trailing underscores,
 4. disallow adjacent underscores (i.e., there must be digits
    both to the left and the right of each underscore).

Thus 1_000_000 and 0xFABC_DEF0 are valid, but 1__000_000 and
0_x_FABC_DEF0_ are not.  Such a proposal would not break any
existing code, nor would it unduly burden real lexical analyzers.

-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Matt Austern <austern@sgi.com>
Date: 1999/04/10
Raw View
David R Tribble <dtribble@technologist.com> writes:

> What's wrong with 'TwoDPhonon', or for that matter, 'Phonon2d'?
> Of course, you might want to call it '_2dPhonon', but this
> breaks the rule about leading underscores for reserved names.
> How about 'a2dPhonon' or 'A2dPhonon'?  Surely you can come up
> with a reasonable sounding name, even within the lexical
> constraints of the language.

_2dPhonon is a legitimate user identifier, provided that it is not
defined within the global namespace.

Reserved identifiers are ones that either
 -- contain two consecutive underscores anywhere in the name; or
 -- begin with an underscore followed by a capital letter; or
 -- begin with an underscore and is defined in the global namespace
    or namespace std.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/04/07
Raw View
On 07 Apr 99 15:37:08 GMT, Gabriel Dos_Reis <gdosreis@korrigan.inria.fr> wrote:
>sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran) writes:

>| In the next revision of C++, it would be nice if we could have
>| variables beginning with numbers.  Eg,
>|    class 2dPhonon { ... };
>| Hope everyone agrees!
>
>I can hardly agree with you, with so few argumentation.

What more is there to say?  When speaking to my friends I say "two
dimensional phonon".  Hence, I like to call the class "2dPhonon".
But because names can't begin with numbers, I have to call the class
"Phonon2d".  Which isn't that bad, although I do prefer the more
intuitive sounding "2dPhonon".

The change is just a syntactic one, so it doesn't impact any of
the complex C++ mechanisms already in place.  Also, I don't think
there are any problems created by allowing names to begin with
numbers.

Of course, if a name begins with a number, it must be followed
eventually by a letter.  This way the parser knows whether the
chars denote a number or a name.  Eg, "2dPhonon" is a name, as
is "2d".  But "2" is a number.  In BNF, the old rule:
   name  = letter+name2
   name2 = letter+name2 | digit+name2 | null
In BNF, the new rule:
   name  = letter+name2 | digit+name3
   name2 = letter+name2 | digit+name2 | null
   name3 = letter+name2 | digit+name3


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "J.Barfurth" <techview@bfk-net.de>
Date: 1999/04/07
Raw View
Siemel Naran schrieb in Nachricht ...
>The change is just a syntactic one, so it doesn't impact any of
>the complex C++ mechanisms already in place.  Also, I don't think
>there are any problems created by allowing names to begin with
>numbers.
>
>Of course, if a name begins with a number, it must be followed
>eventually by a letter.  This way the parser knows whether the
>chars denote a number or a name.  Eg, "2dPhonon" is a name, as
>is "2d".  But "2" is a number.  In BNF, the old rule:
>[snip BNF]

What do you expect "2L" or "3f" to be parsed as ?
How about "0x12345678" ?

IMHO the restrictions that would have to be put on 'names beginning with =
a
_number_' (_digit_ actually would be more concise) in order not to impact
these (IMHO not so complex) 'C++ mechanisms', would be too complex to mak=
e
for a desirable change.
Your BNF would need *many* more rules to satisfy these constraint. I don'=
t
think that would be appropriate for such a basic element of grammar as a
name.

-- J=F6rg
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Patrick Smith <patsmith@pobox.com>
Date: 1999/04/07
Raw View
Siemel Naran wrote:
> >| In the next revision of C++, it would be nice if we could have
> >| variables beginning with numbers.  Eg,
> >|    class 2dPhonon { ... };

....

> Of course, if a name begins with a number, it must be followed
> eventually by a letter.  This way the parser knows whether the
> chars denote a number or a name.  Eg, "2dPhonon" is a name, as
> is "2d".  But "2" is a number.  In BNF, the old rule:

It's not so simple.  Bear in mind that these are numbers:

 17L
 0X0
 0XFEED

--
patsmith@pobox.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Paul Black <paul@canix.co.uk>
Date: 1999/04/07
Raw View
sbnaran@fermi.ceg.uiuc.edu (Siemel Naran) wrote:
> Of course, if a name begins with a number, it must be followed
> eventually by a letter.  This way the parser knows whether the
> chars denote a number or a name.  Eg, "2dPhonon" is a name, as
> is "2d".  But "2" is a number.

Why should 2d be a name but 2l or 2u be a number?

Paul
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/04/08
Raw View
sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran) writes:

>In the next revision of C++, it would be nice if we could have
>variables beginning with numbers.  Eg,
>   class 2dPhonon { ... };
>Hope everyone agrees!

What should be the result of the following program?

#include <iostream>
int main()
{
    int 0xabcd = 5;
    int 0777 = 6;
    int 1 = 0xabcd;
    int 2 = 0777;
    std::cout << 1+2 << std::endl;
}

I think the proposed extension needs more thought. :-)

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Ed Brey" <brey@afd.mke.etn.com>
Date: 1999/04/08
Raw View
A problem arises from conflicts with numbers that contain letters:

Is 0x0 my hug-and-kiss variable or hex 0?
Is 1UL my first Underwriter's Laboratory variable or unsigned long 1?

You could make a rule that in a conflict, number is chosen over variable.
This kind of rule is easy to implement (at least with flex, anyway).

The possiblity for conflict can lead to increased errors and make code
harder to read.  On the other hand, by allowing more natural variable names,
it can make the code easier to read, which decreases errors.

Additionally, allowing names to start with numbers will cause future
versions of C++ or languages based on it to break more legacy code if the
new language adds more modifiers to numbers.  Of course, concerned
programmers can protect themselves by simply not not using names that start
with a digit.

In the spirit of C and C++ to allow programmers the flexibility to choose
their conventions and reasonably decide for themselves what is safe and
unsafe, it would seem that names beginning with digits should be allowed.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/04/08
Raw View
In article <slrn7gn2sg.n05.sbnaran@fermi.ceg.uiuc.edu>,
Siemel Naran <sbnaran@KILL.uiuc.edu> wrote:
>Of course, if a name begins with a number, it must be followed
>eventually by a letter.  This way the parser knows whether the
>chars denote a number or a name.  Eg, "2dPhonon" is a name, as
>is "2d".  But "2" is a number.  In BNF, the old rule:

The general problem with this is that it makes it harder to distinguish
identifiers from numbers.  2ea would be an identifier, while 2e2 would be a
number.  2d would be an identifier, but 2l would be a number.

Another issue is that it makes it harder for the language to evolve.  For
instance, if 2i is currently treated as an identifier, it would be harder
for a future version of the standard to specify this as the syntax for an
imaginary number.

Common Lisp tackled this by defining a syntax called "potential numbers".
Any token that's not a potential number is guaranteed to be interpreted as
a symbol in all future versions of the CL standard.  A potential number
satisfies the following requirements:

* It consists entirely of digits, signs (+ or -), ratio markers (/),
decimal points (.), extension characters (^ or _), and number markers (a
letter not adjacent to another letter, such as exponent markers).

* It contains at least one digit (a letter may be a digit if the input base
is greater than 10 if the token doesn't contain a decimal point).

* It begins with a digit, sign, decimal point, or extension character.

* It doesn't end with a sign (this exception is probably there because CL
inherited the Maclisp increment/decrement functions 1+ and 1-, so these
have to be interpreted as symbols).

So, 2dPhonon is a potential number (in fact, it's an integer) in base 36 or
higher, but a symbol in lower bases.

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/04/08
Raw View
On 07 Apr 99 18:05:16 GMT, J.Barfurth <techview@bfk-net.de> wrote:

>What do you expect "2L" or "3f" to be parsed as ?
>How about "0x12345678" ?

Oops.  I forgot about this!  Guess the proposal can't work.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/04/07
Raw View
In the next revision of C++, it would be nice if we could have
variables beginning with numbers.  Eg,
   class 2dPhonon { ... };
Hope everyone agrees!

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/04/07
Raw View
sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran) writes:

| In the next revision of C++, it would be nice if we could have
| variables beginning with numbers.  Eg,
|    class 2dPhonon { ... };
| Hope everyone agrees!

I can hardly agree with you, with so few argumentation.

--
Gabriel Dps Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]