Skip to content

Allow creating setter-names with symbol literals. #301

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

Open
lrhn opened this issue Apr 5, 2019 · 3 comments
Open

Allow creating setter-names with symbol literals. #301

lrhn opened this issue Apr 5, 2019 · 3 comments
Labels
feature Proposed language feature that solves one or more problems small-feature A small feature which is relatively cheap to implement.

Comments

@lrhn
Copy link
Member

lrhn commented Apr 5, 2019

Dart uses symbols to represent "code identifiers" at run-time. It does so for a number of reasons, one being the ability to not retain the source names at run-time unless necessary.

Symbols are introduced by the system in noSuchMethod invocations, as the member name or named-parameter names. Those can be compared to user supplied symbols to recognize a specific member being (mis-)called.

User symbols are created using either the Symbol constructor or the #... symbol literal.

A symbol literal like #_foo represents a library private name. It is not equal to #_foo that is evaluated in a different library, and it the only thing that is equal to the symbols created by calls of members with that private name.

The Symbol constructor, even when called as const, cannot create library private symbol names. (Some implementations did so anyway, but not all).

However, the only way to create setter names (like foo= for the setter set foo(...)) is to use the Symbol constructor. The #... literal syntax does not allow a trailing =.

That means that there is currently no (not horribly complicated) way to create a private setter-name.

We should allow symbol literals ending in a single =, like #foo=.
(We probably should also allow #foo.bar.baz= since the Symbol constructor accepts those, even though it doesn't correspond to any source name, whereas #foo.bar.baz is a library name).

This is a non-breaking change. The grammar of symbol literals becomes:

<symbolLiteral> ::= `#' (<operator> | (<identifier> (`.' <identifier>)*) `='?)

(The only change is the addition of ​`='?).

This does not conflict with any existing syntax because #foo = 42 is already not valid (the left-hand-side is not an assignable expression). For parsing disambiguation, a symbol literal always extend as far as possible, like it already does to disambiguate #foo.hashCode.

@lrhn lrhn added the feature Proposed language feature that solves one or more problems label Apr 5, 2019
@eernstg
Copy link
Member

eernstg commented Apr 5, 2019

If we have any intentions at all to go in the direction where the "native syntax" for symbols, that is, the syntax using #, should only create symbols for declared identifiers in the current scope then we might just as well avoid the '.'s.

@leafpetersen
Copy link
Member

@eernstg
Copy link
Member

eernstg commented Jul 10, 2019

The current Dart.g updated with

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

parses all of language_2 and subdirs with no issues. So there is no parsing problem in allowing symbol literals like #_foo=.

@lrhn lrhn added the small-feature A small feature which is relatively cheap to implement. label Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems small-feature A small feature which is relatively cheap to implement.
Projects
None yet
Development

No branches or pull requests

3 participants