Skip to content

How do if(<expression>) guard patterns interact with array/object pattern caches? #321

Open

Description

@gibson042

Consider the following:

function* makeIterable() {
  yield 1;
}
match({ iterable: makeIterable() }) {
    when let subject and { iterable: let iter } and iter is [1] and if(subject.iterable is [1]): console.log("fully cached");
    when { iterable: let iter } and iter is [1] and if(iter is [1]): console.log("partially cached");
    default: console.log("uncached");
}

What will be logged—"fully cached" because the if expression uses the cached results for both getting subject.iterable and for getting an iterator for that, "partially cached" because the if expression does not use cached results for the first Get but still remembers results from the iterator, or "uncached" because e.g. the if expression is independent of caching?

Activity

  1. ljharb commented on Mar 18, 2024

    @ljharb
    Member

    I would expect "fully cached" - any implicit iteration done by patterns should be populating and using the cache.

  2. ljharb commented on Sep 6, 2024

    @ljharb
    Member

    On a rethink, it wouldn't make any sense to do this - I think the if part is "just javascript" and the caching doesn't apply there. If you want the cache, use a binding in the pattern.

  3. gibson042 commented on Sep 7, 2024

    @gibson042
    MemberAuthor

    Does your rethink lead you to "partially cached" or to "uncached"?

  4. ljharb commented on Sep 7, 2024

    @ljharb
    Member

    Uncached. Caching only applies in patterns.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions