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 trailing = in symbol literals #13640

Open
rmacnak-google opened this issue Sep 26, 2013 · 6 comments
Open

Allow trailing = in symbol literals #13640

rmacnak-google opened this issue Sep 26, 2013 · 6 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug

Comments

@rmacnak-google
Copy link
Contributor

symbolLiteral:
‘#’ (operator | (identifier (‘.’ identifier)*) '='?)

Would be nice to say
classMirror.members[#setter=]

@gbracha
Copy link
Contributor

gbracha commented Sep 26, 2013

Added Accepted label.

@DartBot
Copy link

DartBot commented Sep 27, 2013

This comment was originally written by @mhausner


I proposed to write Symbol literals as #"abc" instead of #abc. This would make #"abc=" a regular case, and we could also express operator symbols as literals. I think it also expresses better what a symbol literal is: a string (with some special semantics for compilers). Lastly, it is similar syntax to raw strings.

Thinking about the last point some more, we could also define symbol literals as s"abc". But I won't push my luck any further here.

@DartBot
Copy link

DartBot commented Sep 27, 2013

This comment was originally written by @seaneagan


String syntax might make people think they can put any String, not just identifiers, and that it returns an actual String, as opposed to a Symbol.

What do you mean by "regular case"?

Why couldn't operator symbols be exposed with (an extension of) the current syntax?

#()
#<
#<=
#==
#+
#-
// ...

unary - is always tricky, maybe:

#-1

@DartBot
Copy link

DartBot commented Sep 27, 2013

This comment was originally written by @seaneagan


Oh, my bad, looks like the spec already allows operator symbol literals!

@rmacnak-google rmacnak-google added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Sep 27, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed priority-unassigned labels Feb 29, 2016
@lrhn
Copy link
Member

lrhn commented Jan 12, 2024

Still valid, there is no way to express the symbol for a private setter. You can get a private getter name as #_foo, an a non-private setter name as const Symbol("foo="), but there is no way to create _foo= as a constant symbol.

You need an elaborate noSuchMethod-based approach to create such a symbol at runtime, which makes it ineligible for being used in a switch case.

void main() {
  print(#_foo);
  print(const Symbol("foo="));
  print(_fooSetSymbol);
}

final Symbol _fooSetSymbol = symbolOf((d) => d._foo = 0);

// Imagine it's in another library, so `_last` won't get in the way.
Symbol symbolOf(void Function(dynamic target) access) {
  var s = _Symbolizer();
  access(s);
  return s._last ?? (throw StateError("No member access"));
}

class _Symbolizer {
  Symbol? _last;
  noSuchMethod(Invocation invocation) {
    _last = invocation.memberName;
  }
}

@eernstg
Copy link
Member

eernstg commented Jan 12, 2024

Right, but then I can see that we need an issue for dart2js: #54603.

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). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants