Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ! and ? in method and variable names #6438

Closed
DartBot opened this issue Oct 31, 2012 · 9 comments
Closed

Allow ! and ? in method and variable names #6438

DartBot opened this issue Oct 31, 2012 · 9 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Oct 31, 2012

This issue was originally filed by jjinux...@google.com


It'd be great if we could let people use ? and ! in variable names. This is very helpful and popular in some languages such as Ruby and Lisp. For instance, list.sort() returns a sorted version of the list, whereas list.sort!() sorts the list in place. Here's another example: active? is the name of a boolean getter.

(This was Shailen Tuli's idea.)

@DartBot
Copy link
Author

DartBot commented Oct 31, 2012

This comment was originally written by jjinux...@google.com


This would add a little bit of syntactic spiciness to the language without making it completely foreign to Java and JavaScript programmers.

There is one case where it isn't 100% backwards compatible. If someone writes a ternary expression such as "a? 0: 1", "a?" will be treated as a variable, thus resulting in both a variable that is not in scope error and a syntactic error. I don't think that little bit of backwards incompatability should stop us from doing this, though, and it will be very easy to fix such problems since they'll result in loud errors.

@gbracha
Copy link
Contributor

gbracha commented Oct 31, 2012

Added Accepted label.

@lrhn
Copy link
Member

lrhn commented Nov 1, 2012

Are you suggesting that whitespace should be significant, so "a ?" is different from "a?"?
I fear that would be very surprising to Java and JavaScript (and any other C-syntax-language) programmers, where quickly writing "a<b?a:b" without spaces is expected to work.
(Not that I would mind being able to have such variable names, as an old Schemer).

@DartBot
Copy link
Author

DartBot commented Nov 2, 2012

This comment was originally written by jjinux...@google.com


It's not so much that whitespace is significant. It's just that "a?" lexes as a single token. Hence, if you don't want the ? to be part of the token, you do need to use whitespace. There are other instances of this in the language already. For instance, you can't get rid of the " " in "new Map".

I think this will cause less confusion than the benefits it brings.

@DartBot
Copy link
Author

DartBot commented Nov 2, 2012

This comment was originally written by rakudrama...@gmail.com


If we are adding interesting characters to variable names, as an old ML programmer, I'd like to add a prime, as in:

var a = ...;
var a' = f(a);
var a'' = f(a');

I would rather have ? reserved for nullable types, though.

@DartBot
Copy link
Author

DartBot commented Nov 2, 2012

This comment was originally written by jjinux...@google.com


I wouldn't mind support for a', although that could be confusing with r'foo' meaning something special. As much as I like non-nullable types, I suspect we shouldn't hold off on using ? in variable names just in case we one day happen to support non-nullable types and decided to use ? for them.

@DartBot
Copy link
Author

DartBot commented Jan 3, 2013

This comment was originally written by shailen...@gmail.com


I would say that ? and ! would be very useful, but if I had to have only one of those, I would pick !.
? is a useful suffix for predicates in Ruby and Scheme, but its easy to prefix a function with 'is' (or something similar) and get pretty much the same result.

The common semantics of ! - modifying the receiver if present, not modifying if not- is highly useful in creating nice APIs (myList.sort vs myList.sort!, nestedList.flatten vs nestedList.flatten!) and it would be great if we could find a way to add it to Dart.

@Zectbumo
Copy link

-1 This sounds like a bad idea.
Comment #­4 , you can't compare syntax text new with symbol ?. Of course white space matters when it comes to text. Symbols clearly don't work the same.

As for !, how about a!=54, am I setting a variable named a! or am I checking if a is not equal to 54? Just forget it guys, this is a nonstarter.

@gbracha
Copy link
Contributor

gbracha commented Jan 2, 2015

Set owner to @gbracha.
Added WontFix label.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jan 2, 2015
@kevmoo kevmoo added closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug and removed resolution-wont_fix labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants