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

Property extraction in form 'e.m' does not work for operators #24557

Closed
sgrekhov opened this issue Oct 12, 2015 · 6 comments
Closed

Property extraction in form 'e.m' does not work for operators #24557

sgrekhov opened this issue Oct 12, 2015 · 6 comments
Assignees
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

@sgrekhov
Copy link
Contributor

Consider the following code:

class C {
  bool operator > (var v) {
    print('called');
    return true;
  }
}

main() {
  C c = new C();
  var f = c.>;
  f(1);
}

This code doesn't work. Result is:

error:  identifier expected
  var f = c.>;
            ^

Bit if to replace

var f = c.>;

by

var f = c#>;

then it works well.

But where in the spec it is said that 'e.m' syntax should not extract operators?

According the specification (3-rd edition, June 2015) reads (16.18.1 Getter Access and Method Extraction):

"Evaluation of a property extraction i of the form e.m proceeds as follows: First, the expression e is evaluated to an object o. Let f be the result of looking up (16.15.1) method (10.1) m in o with respect to the current library L. If o is an instance of Type but e is not a constant type literal, then if f is a
method that forwards (9.1) to a static method, method lookup fails. If method lookup succeeds then i evaluates to the closurization of method f on object o"

According " 16.18.3 General Closurization"

"Evaluation of a property extraction i of the form e#m proceeds as follows:
...
If m is not a setter name, let f be the result of looking up method m
in o with respect to the current library L. If o is an instance of Type but e
is not a constant type literal, then if f is a method that forwards to a static
method, method lookup fails. If method lookup succeeds then i evaluates to the
closurization of method f on object o"

There are no any difference between these statements regarding operators. Either specification should be changed to clearly state that 'e.m' syntax should not extract operators either implementation should extract operators as well.

@rmacnak-google rmacnak-google added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Oct 12, 2015
@sgrekhov
Copy link
Contributor Author

What is correct behavior? Should operators be extracted by e.m syntax as well as by e#m?

@floitschG floitschG assigned floitschG and gbracha and unassigned floitschG Oct 13, 2015
@floitschG
Copy link
Contributor

According to the spec (16.32) o.m is an assignable expression with .. Therefore o.< is syntactically not allowed.

Section 16.18.7, on the other hand, explicitly talks about the operators.

I believe this is working as intended (but will let Gilad close the bug).

@lrhn
Copy link
Member

lrhn commented Oct 13, 2015

The grammar in question is:

postfixExpression:
    assignableExpression postfixOperator |
    primary (selector* | ( ‘#’ ( (identifier ‘=’?) | operator)))
    ;
selector: 
    assignableSelector |
    arguments
    ;
assignableSelector:
    unconditionalAssignableSelector |
    ‘?.’ identifier
    ;
unconditionalAssignableSelector:
    ‘[’ expression ‘]’ |
    ‘.’ identifier
    ;

Basically, the only thing allowed after a . is an identifier, so o.< is not valid syntax.

The closurization of operators can currently only be achieved using # notation: o#< is valid syntax, and section 16.18.7 describes its semantics.

@sgrekhov
Copy link
Contributor Author

Ok, thank you for clarification. But it's not obvious. I think it makes sense to add to "16.18.1 Getter Access and Method Extraction" statement or, at least, non-normative section, describing that property extraction in form e.m doesn't work for operators

@sgrekhov
Copy link
Contributor Author

Another place in the specification that should be changed "16.18.7 Ordinary Member Closurization"
"Observations:
One cannot closurize a constructor, getter or a setter via the dot based syntax.
One must use the # based form."
Operators must be added to the list.

@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
@munificent
Copy link
Member

Generalized tear-offs are being removed (#27518) so I don't think we have to worry about cleaning up 16.18.7. For the o.< syntax, I think the current spec is OK, so I'm going to close this. Feel free to reopen if I have that wrong.

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

7 participants