This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala alphanumeric identifiers and '$' character

11 replies
Primož Kokol
Joined: 2012-01-13,
User offline. Last seen 42 years 45 weeks ago.

From Programming in Scala section 6.10 (Page 151):

------------------
Identifiers in user programs should not contain '$' character, even
though it will compile; if they do this might lead to name clashes
with identifiers generated by Scala compiler.
------------------

I am sure it's a reason for this, but why not prevent use of the '$'
character in alphanumeric identifiers?

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Scala alphanumeric identifiers and '$' character
Suppose that some other language also decides to use $ for name mangling.  How would Scala code refer to them if one could not write $ in a method name?

Rather than assume that it knows best, the compiler trusts you to be sensible.

  --Rex

2012/1/13 Primož Kokol <primoz [dot] kokol [at] gmail [dot] com>
From Programming in Scala section 6.10 (Page 151):

------------------
Identifiers in user programs should not contain '$' character, even
though it will compile; if they do this might lead to name clashes
with identifiers generated by Scala compiler.
------------------

I am sure it's a reason for this, but why not prevent use of the '$'
character in alphanumeric identifiers?

Primož Kokol
Joined: 2012-01-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala alphanumeric identifiers and '$' character

Thanks for answer.

On Jan 13, 4:18 pm, Rex Kerr wrote:
> Suppose that some other language also decides to use $ for name mangling.
> How would Scala code refer to them if one could not write $ in a method
> name?
>
> Rather than assume that it knows best, the compiler trusts you to be
> sensible.
>
>   --Rex
>
> 2012/1/13 Primož Kokol
>
>
>
>
>
>
>
> > From Programming in Scala section 6.10 (Page 151):
>
> > ------------------
> > Identifiers in user programs should not contain '$' character, even
> > though it will compile; if they do this might lead to name clashes
> > with identifiers generated by Scala compiler.
> > ------------------
>
> > I am sure it's a reason for this, but why not prevent use of the '$'
> > character in alphanumeric identifiers?

Justin du coeur
Joined: 2009-03-04,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala alphanumeric identifiers and '$' character
2012/1/13 Rex Kerr <ichoran [at] gmail [dot] com>
Suppose that some other language also decides to use $ for name mangling.  How would Scala code refer to them if one could not write $ in a method name?

Rather than assume that it knows best, the compiler trusts you to be sensible.

That's fair, but now I'm wondering if this at least produces a warning.  (Don't have it installed here at work to try it out.)  
It's the sort of thing that should be allowed if you're a sophisticated user doing it deliberately, but the naive user who is doing it entirely unknowing (which I'd bet is the 99% case) is best protected from doing so by accident.  So it strikes me that a suppressible warning is the right response to this...
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Scala alphanumeric identifiers and '$' character


On Fri, Jan 13, 2012 at 11:15 AM, Justin du coeur <jducoeur [at] gmail [dot] com> wrote:
2012/1/13 Rex Kerr <ichoran [at] gmail [dot] com>
Suppose that some other language also decides to use $ for name mangling.  How would Scala code refer to them if one could not write $ in a method name?

Rather than assume that it knows best, the compiler trusts you to be sensible.

That's fair, but now I'm wondering if this at least produces a warning.  (Don't have it installed here at work to try it out.)  
It's the sort of thing that should be allowed if you're a sophisticated user doing it deliberately, but the naive user who is doing it entirely unknowing (which I'd bet is the 99% case) is best protected from doing so by accident.  So it strikes me that a suppressible warning is the right response to this...

The chance that you'll break something as a naive user is also pretty small--you'd have to collide with the name mangling scheme Scala uses.  I'd prefer that the compiler not assume you don't know what you're doing unless you're very likely to have made a mistake (e.g. incomplete pattern match in a position that calls for a complete one).

  --Rex

Justin du coeur
Joined: 2009-03-04,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala alphanumeric identifiers and '$' character
On Fri, Jan 13, 2012 at 11:46 AM, Rex Kerr <ichoran [at] gmail [dot] com> wrote:
The chance that you'll break something as a naive user is also pretty small--you'd have to collide with the name mangling scheme Scala uses.  I'd prefer that the compiler not assume you don't know what you're doing unless you're very likely to have made a mistake (e.g. incomplete pattern match in a position that calls for a complete one).

Obviously subjective, but I don't agree.  Using $ in an identifier is a just plain bad idea -- it begs for name-mangling errors that can be introduced later by accident -- so while I agree that it's unlikely, I think that it's worth discouraging.
(That is, I think you've made a good point that it should be *allowed*, but I still don't think it should ever be recommended practice unless you must...) 
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Scala alphanumeric identifiers and '$' character


On Fri, Jan 13, 2012 at 9:19 AM, Justin du coeur <jducoeur [at] gmail [dot] com> wrote:
Obviously subjective, but I don't agree.

I don't agree either, and I'm not sure it's subjective.  It's way easier than you guys probably think to cause a behaviorial change with a $ in your name.  The problems I anticipate with warning about it are that we lack any decent mechanism for suppressing specific warnings in a wide swathe, and that I imagine in one or more somewheres there will be generated code which turns up looking like user code and we will have spurious warnings.  (Like, I'd have to turn it off completely in the repl, as would anything with a repl-like mechanism.)
If these were not problems I would have no reservation about warning about it, because I cringe every time I hear someone suggest using a $ for anything but matters of currency far far from scala.
Justin du coeur
Joined: 2009-03-04,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala alphanumeric identifiers and '$' character
On Fri, Jan 13, 2012 at 2:27 PM, Paul Phillips <paulp [at] improving [dot] org> wrote:
The problems I anticipate with warning about it are that we lack any decent mechanism for suppressing specific warnings in a wide swathe

Interesting -- that honestly hadn't occurred to me, since I'm used to fairly often using such mechanisms in other languages; I had simply assumed that the mechanism existed.  I agree that this is a significant concern...
Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala alphanumeric identifiers and '$' character
Let's not forget that Java allows you to have $ characters in your identifiers -- and discourages them for the same reason. 

I have never *once* seen anyone actually use a dollar sign in a Java identifier.  Unless Scala programmers are demonstrably naughtier than Java programmers, I can't really see special preventative measures being any kind of priority.

Ken
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Scala alphanumeric identifiers and '$' character

On Fri, Jan 13, 2012 at 22:10, Ken Scambler wrote:
> Let's not forget that Java allows you to have $ characters in your
> identifiers -- and discourages them for the same reason.
>
> I have never *once* seen anyone actually use a dollar sign in a Java
> identifier.  Unless Scala programmers are demonstrably naughtier than Java
> programmers, I can't really see special preventative measures being any kind
> of priority.

Java programmers have no reason to suspect it would be allowed, while
Scala programmers have no reason to suspect it could be forbidden.

And I did see people using $ in identifiers, both Java and Scala.
Sadly, more often in Scala, and even despite being aware they
shoudln't.

Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Scala alphanumeric identifiers and '$' character
I'm pretty sure the default GUI builders of some IDEs ~10 years ago used to use $ as part of (generated) source code.

Date: Sat, 14 Jan 2012 11:10:35 +1100
Subject: Re: [scala-debate] Scala alphanumeric identifiers and '$' character
From: ken [dot] scambler [at] gmail [dot] com
To: jducoeur [at] gmail [dot] com
CC: paulp [at] improving [dot] org; ichoran [at] gmail [dot] com; primoz [dot] kokol [at] gmail [dot] com; scala-debate [at] googlegroups [dot] com

Let's not forget that Java allows you to have $ characters in your identifiers -- and discourages them for the same reason. 

I have never *once* seen anyone actually use a dollar sign in a Java identifier.  Unless Scala programmers are demonstrably naughtier than Java programmers, I can't really see special preventative measures being any kind of priority.

Ken
Primož Kokol
Joined: 2012-01-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala alphanumeric identifiers and '$' character

Wow.. Didn't know this question will rise such a debate.. :) Thanks
for all great answers!!

I have one more question. More than anything else to clarify other
things (for example LITERAL IDENTIFIERS) in my head:

Is it TRUE, that even if the use of `$` character in ALPHANUMERIC
IDENTIFIERS would be prevented, you could still access the mangled
variables of other languages through LITERAL IDENTIFIERS?

On Jan 14, 5:23 pm, Chris Marshall wrote:
> I'm pretty sure the default GUI builders of some IDEs ~10 years ago used to use $ as part of (generated) source code.
>
> Date: Sat, 14 Jan 2012 11:10:35 +1100
> Subject: Re: [scala-debate] Scala alphanumeric identifiers and '$' character
> From: ken [dot] scamb [dot] [dot] [dot] [at] gmail [dot] com
> To: jduco [dot] [dot] [dot] [at] gmail [dot] com
> CC: pa [dot] [dot] [dot] [at] improving [dot] org; icho [dot] [dot] [dot] [at] gmail [dot] com; primoz [dot] ko [dot] [dot] [dot] [at] gmail [dot] com; scala-debate [at] googlegroups [dot] com
>
> Let's not forget that Java allows you to have $ characters in your identifiers -- and discourages them for the same reason.
>
> I have never *once* seen anyone actually use a dollar sign in a Java identifier.  Unless Scala programmers are demonstrably naughtier than Java programmers, I can't really see special preventative measures being any kind of priority.
>
> Ken

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland