Skip to content

Feature: Don't care/anonymous parameter #5586

Closed

Description

@qc00

Many functional languages have a don't care parameter/variable that holds values not used by programs.

For example, a function like Array.prototype.forEach supplies three arguments to the callback. If the code only needs the second argument (index), it will still have to supply a name for the first one. The programmer can use a don't care parameter as the name of the first argument instead. This improves readability and avoids problem of accidentally shadowing.

An additional keyword/comment may have to be introduced to define which string is used for this purpose as the traditional "_" happens to be used by the popular underscore library.

This can also be extended to destructuring code.

Activity

  1. changed the title [-]Don't care/anonymous parameter[/-] [+]Feature: Don't care/anonymous parameter[/+] on Nov 10, 2015
  2. RyanCavanaugh commented on Nov 10, 2015

    @RyanCavanaugh
    Member

    Just thinking aloud -- would arr.forEach(( , index) => console.log(index)); look too much like a typo?

  3. sandersn commented on Nov 10, 2015

    @sandersn
    Member

    I think so. Also, when commas stack up they get hard to count.

    Does the already-supported workaround of _, __, __, ... suffice?

  4. FabianLauer commented on Nov 10, 2015

    @FabianLauer

    I agree with Nathan Shively-Sanders (@sandersn). How about arr.forEach((void, index) => console.log(index));?
    Just the first thing that came to mind. Correct me if I'm wrong, but I think this doesn't lead to a conflict with the ES spec as it would be a part of the type system.

  5. coreh commented on Dec 3, 2015

    @coreh

    Does the already-supported workaround of _, __, __, ... suffice?

    Maybe _ could be special cased, so that you can use it multiple times, then __ and ___ wouldn't be needed.

    (_, __, x, ____) => x + 1

    Would become

    (_, _, x, _) => x + 1
  6. LiamGoodacre commented on Dec 20, 2015

    @LiamGoodacre

    How about ?? For example: (?, x, ?) => x + 1.
    Given that _ is a valid identifier name, I don't believe it is appropriate to use here.

  7. DanielRosenwasser commented on Dec 20, 2015

    @DanielRosenwasser
    Member

    You could just use an empty binding pattern like[] or {}.

    arr.forEach(({}, index) => console.log(index));
    
    arr.forEach(([], index) => console.log(index));

    Since they are not disallowed (see tc39/ecma262#97), I'm inclined to close this unless there's a problem with that workaround.

  8. LiamGoodacre commented on Dec 20, 2015

    @LiamGoodacre
  9. qc00 commented on Dec 20, 2015

    @qc00
    Author

    Daniel Rosenwasser (@DanielRosenwasser) Sounds perfect. Would be nice if this special use case is documented though.

  10. FabianLauer commented on Dec 21, 2015

    @FabianLauer
  11. added
    Out of ScopeThis idea sits outside of the TypeScript language design constraints
    and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Dec 21, 2015
  12. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions