Skip to content

Design feedback from JSCIG #259

Closed as not planned
Closed as not planned

Description

@Jack-Works

Today I presented the current syntax and semantics to JSCIG and got the following feedback.

Still looks similar to switch

  1. The default keyword (but we cannot use else)
  2. : as the separate token (looks like case x:)
  3. Current design has a strong visual hint (:) that the RHS can be a statement but it actually cannot.

Should nest match shares the same cache?

const it = [0, 1, 2].values()
match (it) {
    when ([a, b, c]): match (it) {
        when ([a, b, c]): void 0 // can we reach here?
    }
}

@@matcher is a terrible name.

And even renaming it to something else is not solving the problem, because we cannot remove @@match and people will keep stuck on it.

How built-in @@matcher work exactally?

e.g. How Error@@matcher work?

Better try-catch?

try {}
catch when (pattern) {}
catch if (expr) {}
catch when (pattern) {}
catch {}

Activity

  1. ljharb commented on Jun 16, 2022

    @ljharb
    Member

    I agree on default - I'd strongly prefer an alternative.

    I don't think : matters because object literals already have that, as does TS types.

    : in the language (and in TS) is only followed by a statement with a label, but only an expression everywhere else, and since nobody uses labels, that seems like not something worth worrying about.

  2. ljharb commented on Jun 16, 2022

    @ljharb
    Member

    As for "better try-catch", that's only something a followup proposal should do - see #239, #144, #18, #14.

  3. ljharb commented on Jun 16, 2022

    @ljharb
    Member

    Error[Symbol.matcher] would check for the presence of the [[ErrorData]] internal slot, I assume.

  4. tabatkins commented on Jun 16, 2022

    @tabatkins
    Collaborator

    I continue to not have strong opinions on the default name, but we already decided against else due to it visually chaining with a preceding if() matcher. There's not a lot else we can do besides default if we don't have an official nil-matcher. (That is, I think we could remove default if when(_) was an auto-match that didn't generate a binding.)

    And yeah, : doesn't suggest a statement anywhere besides switch; it carries an expression everywhere else in the language.

    (In general, tho, I don't have a problem with us slightly overlapping with switch syntactically, even if we have differences like this. It's fine, this is a full switch replacement, it's something to learn once and never worry about again.)

    Error[Symbol.matcher] would check for the presence of the [[ErrorData]] internal slot, I assume.

    Yes, whatever the theoretically equivalent of Error.isError would do, which is gonna be some variety of brand check. The subclasses would brand-check for their specific subclass, etc.

    And yeah, I feel strongly that integrating pattern-matching with try/catch is something that would be good to do, but in a follow-up; it's not part of the core syntax and it's got enough complexity that it would drag the main proposal.

  5. tabatkins commented on Jun 16, 2022

    @tabatkins
    Collaborator

    Should nest match shares the same cache?

    Strongly inclined to say no. Using it for anything else in the RHS will show it already advanced an appropriate amount; it seems like it would be really confusing if using it in a match construct seemed to "rewind" it. Fragile, too, since you can do stuff like it.next() && match(it) {...} in the RHS.

  6. noppa commented on Jun 16, 2022

    @noppa

    I think we could remove default if when(_) was an auto-match that didn't generate a binding

    Has this been explored with matchers that would not be valid bindings anyway? E.g. when(else)

    match (res) {
      when ({ status: 200 }) { ok(); }
      when ({ status }) if (400 <= status && status < 500) { err(); }
      when (else) { noResponse(); }
    }
  7. ljharb commented on Jul 8, 2022

    @ljharb
    Member

    We should discuss further, but I think this doesn't need to block stage 2.

  8. fuchsia commented on Oct 22, 2022

    @fuchsia

    Another choice of color for the bikeshed:

    How about do instead of : And * has plenty of precedent as a wildcard. So it becomes:

    match (res) {
      when ({ status: 200 }) do ok();
      when ({ status }) if (400 <= status && status < 500) do err();
      when (*) do noResponse(); 
    }

    If do-expressions ever arrive, then you can incorporate them.

    That avoids setting up semantics for blocks that ends up being different to those used in do-expressions. Hive off the arguments to that proposal.

  9. ljharb commented on Oct 22, 2022

    @ljharb
    Member

    This proposal will not advance past do expressions; it depends on them.

  10. Jack-Works commented on Sep 4, 2023

    @Jack-Works
    MemberAuthor

    close for now because we're redesign things

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions