Skip to content

proposal: spec: accept x[len-1] as x[len(x)-1] #20176

Closed
@posener

Description

@posener

When applying the index or slice operations on a string/array/slice allow the keyword len inside the brackets. In that case len will mean len(a) where a is the referred string/array/slice variable before the brackets.

For example, if s is of string/array/slice type:

  • s[len-1] will be exactly the same as s[len(s)-1]
  • s[len-5:len-2] will be exactly the same as s[len(s)-5:len(s)-2]

Pros:

  • Readability, specially when the variable name is long.
  • Does not add any new keyword to the language.

Cons:

  • Will break programs that use len as variable name.

Related issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions