Skip to content

Feature Request: switch tuple #9419

Closed
Closed

Description

@frogcjn

for example:

const tuple: [number, number] = [f0.length, f1.length]

switch (tuple) {
    case [3,3]: return ...
    case [3,4]: return ...
    case [4,3]: return ...
    case [4,4]: return ...
    default: throw Error("Invalid Case")
}

currently, tuple will not match any case, though its value is [3, 3]

Activity

  1. mortb commented on Jun 29, 2016

    @mortb

    If you can't wait for the team to answer, here is a workaround:

    switch (tuple.join(",")) {
        case "3,3": return ...
        case "3,4": return ...
        case "4,3": return ...
        case "4,4": return ...
        default: throw Error("Invalid Case")
    }
    
  2. sandersn commented on Jun 29, 2016

    @sandersn
    Member

    I don't think TypeScript can help in this case because switch working with arrays would be quite different from JavaScript's behaviour. You probably should support the ECMAScript pattern matching proposal. If that's accepted into the standard, you will get tuple matching and more.

  3. frogcjn commented on Jun 29, 2016

    @frogcjn
    Author

    Nathan Shively-Sanders (@sandersn)
    does ECMAScript have tuple?
    I did not proposal array match, but tuple.

    If the programming language has a "tuple" concept, it means it can match with other tuple.
    There is nothing to do with array, though you use array to simulate tuple.

  4. sandersn commented on Jun 29, 2016

    @sandersn
    Member

    You just need array matching to get the semantics you want. Tuples are just a type to make sure that you aren't matching against nonsensical patterns.

    Of course if ES20xx adds support for pattern matching, then TypeScript will allow you to type arrays as tuple in a matching context.

  5. frogcjn commented on Jun 29, 2016

    @frogcjn
    Author

    Martin Böh (@MartB), thanks. I solved it what as you writes.
    But as I suggest, typescript has a "tuple" concept, then it should has an ability to match tuple in switch.

  6. frogcjn commented on Jun 29, 2016

    @frogcjn
    Author

    Nathan Shively-Sanders (@sandersn) OK. I'll proposal ECMAScript has all type features like typescript. And then I'll wait until 2019.

  7. sandersn commented on Jun 29, 2016

    @sandersn
    Member

    Pattern matching isn't really a type-system concept. There are plenty of languages with pattern matching but no compile-time types. (Erlang, Clojure, Common Lisp and Scheme come to mind immediately.)

  8. kitsonk commented on Jun 29, 2016

    @kitsonk
    Contributor

    But as I suggest, typescript has a "tuple" concept, then it should has an ability to match tuple in switch.

    JavaScript does not switch on non-primatives (references). You cannot switch on Objects, Arrays, etc..

    While a tuple type is a TypeScript concept, it is still a non-primative concept. As tuples stand at the moment, they are a design time only concept, with their typing information fully erasable by using Array literals at run-time. Implementing this feature it would break several of the TypeScript Design Goals:

    1. Impose no runtime overhead on emitted programs.
    2. Preserve runtime behavior of all JavaScript code.

    And would be an anti-goal:

    1. Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.
  9. 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

    ES NextNew featurers for ECMAScript (a.k.a. ESNext)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions