Scala alphanumeric identifiers and '$' character

95 views
Skip to first unread message

Primož Kokol

unread,
Jan 13, 2012, 10:15:55 AM1/13/12
to scala-debate
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?

Rex Kerr

unread,
Jan 13, 2012, 10:18:21 AM1/13/12
to Primož Kokol, scala-debate
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...@gmail.com>

Primož Kokol

unread,
Jan 13, 2012, 10:23:17 AM1/13/12
to scala-debate
Thanks for answer.

On Jan 13, 4:18 pm, Rex Kerr <icho...@gmail.com> 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 <primoz.ko...@gmail.com>

Justin du coeur

unread,
Jan 13, 2012, 11:15:19 AM1/13/12
to Rex Kerr, Primož Kokol, scala-debate
2012/1/13 Rex Kerr <ich...@gmail.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...

Rex Kerr

unread,
Jan 13, 2012, 11:46:32 AM1/13/12
to Justin du coeur, Primož Kokol, scala-debate

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

unread,
Jan 13, 2012, 12:19:23 PM1/13/12
to Rex Kerr, Primož Kokol, scala-debate
On Fri, Jan 13, 2012 at 11:46 AM, Rex Kerr <ich...@gmail.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...) 

Paul Phillips

unread,
Jan 13, 2012, 2:27:28 PM1/13/12
to Justin du coeur, Rex Kerr, Primož Kokol, scala-debate


On Fri, Jan 13, 2012 at 9:19 AM, Justin du coeur <jduc...@gmail.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

unread,
Jan 13, 2012, 2:48:07 PM1/13/12
to Paul Phillips, Rex Kerr, Primož Kokol, scala-debate
On Fri, Jan 13, 2012 at 2:27 PM, Paul Phillips <pa...@improving.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

unread,
Jan 13, 2012, 7:10:35 PM1/13/12
to Justin du coeur, Paul Phillips, Rex Kerr, Primož Kokol, scala-debate
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

Daniel Sobral

unread,
Jan 14, 2012, 10:29:06 AM1/14/12
to Ken Scambler, Justin du coeur, Paul Phillips, Rex Kerr, Primož Kokol, scala-debate

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.

--
Daniel C. Sobral

I travel to the future all the time.

Chris Marshall

unread,
Jan 14, 2012, 11:23:09 AM1/14/12
to ken.sc...@gmail.com, jduc...@gmail.com, pa...@improving.org, ich...@gmail.com, primoz...@gmail.com, scala-...@googlegroups.com
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.sc...@gmail.com
To: jduc...@gmail.com
CC: pa...@improving.org; ich...@gmail.com; primoz...@gmail.com; scala-...@googlegroups.com

Primož Kokol

unread,
Jan 16, 2012, 3:07:39 AM1/16/12
to scala-debate
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 <oxbow_la...@hotmail.com> 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.scamb...@gmail.com
> To: jduco...@gmail.com
> CC: pa...@improving.org; icho...@gmail.com; primoz.ko...@gmail.com; scala-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages