Skip to content

Revise getter/setter syntax #3602

Closed
Closed

Description

@gbracha

Getter declarations no longer require/allow parentheses: int get foo=> 42;

Setter declarations have an = sign after the identifier: set foo=(int x);

The name of a setter for purposes of reflection and noSuchMethod is id=, whereas the getter is just id. This allows a standard, reliable way to distinguish them by their names just like any other method.

All this needs to get into the spec.

Activity

  1. gbracha commented on Jun 13, 2012

    @gbracha
    ContributorAuthor

    The obvious path here is to make the getter parens optional, and likewise the setter = sign. The internal names can be fixed at this time as well. Then fix the libs, then disallow parens on getters and make the = required on setters

  2. gbracha commented on Jun 13, 2012

    @gbracha
    ContributorAuthor

    Note that this also eliminates to compile-time error situations: there is no need to check that the formal parameter list of a getter is empty, since there is no parameter list. Also, a setter name can never conflict witha getter or method name, so no need to check for such a conflict either.

  3. gbracha commented on Jun 13, 2012

    @gbracha
    ContributorAuthor

    Spec'ed in 0.11 draft.


    Added Done label.

  4. gbracha commented on Jun 13, 2012

    @gbracha
    ContributorAuthor

    Analyzer issue #3607.
    VM issue #3608.
    dart2js issue #3609.

  5. DartBot commented on Jun 13, 2012

    @DartBot

    This comment was originally written by @mhausner


    Is get foo { ...statements here...; return value }

    legal syntax, or did we introduce a situation where the two forms of function blocks (=> and { }) are not interchangeable anymore?

    I presume the => in get foo=> 42; is a separate token, not part of the getter name, despite the lack of a space in the example above.

  6. gbracha commented on Jun 13, 2012

    @gbracha
    ContributorAuthor

    get foo { ...}

    is perfectly legal. The example using => is just one example. ANd yes, +> is not part of the getter name - the getter name is always an identifier (at least for now - we are likely to view [] as a getter soon - fun details to follow).

    The 0.11 draft gives the syntax, which is unchanged except that no formal parameter list is allowed in a getter signature, and an extra = sign is introduced before the formal parameter list in a setter signature.

    Once the change is done, there will be fewer compiler checks and things should be ever so slightly simpler and cleaner. Rejoice!

  7. DartBot commented on Jul 10, 2012

    @DartBot

    This comment was originally written by er...@codesmithtools.com


    I really don't get this change. Syntactically, it's an ugly change. You already have the "set" keyword to indicate that it's a setter function, so why do you need to have an = as well? I am guessing there is some logical reason for this choice that has something to do with the mirrors system, but why couldn't function mirrors just include some sort of flag on them like isSetter? I'm guessing because then you'd have two functions in the mirror with the same name and only a boolean flag to differentiate them. Still it seems like the syntax is being uglified because there is an issue in implementing mirrors for it and I don't like the resulting compromise.

  8. DartBot commented on Jul 20, 2012

    @DartBot

    This comment was originally written by @tomaszkubacki


    setter "=" seems really ugly and i agree with #­7 smeels like a bad compromise. Can't mirros really workaround this somehow ?

  9. peter-ahe-google commented on Aug 15, 2012

    @peter-ahe-google
    Contributor

    Alas, no rejoice. AFAIK, the new setter syntax was abandoned. So no = in the setter name.

  10. DartBot commented on Aug 16, 2012

    @DartBot

    This comment was originally written by rodion...@unipro.ru


    then the following paragraph in 3.1:
    It is a compile-time error if there is more than one entity with the same
    name declared in the same scope, with the exception of the operator-, where
    both a unary and a binary version may coexist.

    needs to be changed back to include setters and getters among the exceptions, right?

  11. self-assigned this
    on Aug 16, 2012
  12. added this to the M1 milestone on Aug 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions