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
Comments
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. |
Added Accepted label. |
Are you suggesting that whitespace should be significant, so "a ?" is different from "a?"? |
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. |
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 = ...; I would rather have ? reserved for nullable types, though. |
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. |
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 !. 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. |
-1 This sounds like a bad idea. 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. |
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.)
The text was updated successfully, but these errors were encountered: