Skip to content

Where causes code to be called twice #1181

Open
@Deijin27

Description

@Deijin27

Take the code

class Foo {
  num { _num }
  construct new(num) {
    _num = num
    System.print("Creating foo")
  }
}

var bar = [1, 2].map{|x| Foo.new(x) }.where{|x| x.num > 0}.toList

If run, it prints "Creating foo" 4 times. Intuitively I expect it to call only 2 times, and I tested the same thing using C# linq and it is called 2 times.

Is this possible to fix with how WhereSequence works?
At the very least it's something to look out for in case your construction is resource intensive.

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