Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-9447] Lazy operations from sequence extensions aren't lazy #51911

Closed
swift-ci opened this issue Dec 8, 2018 · 7 comments
Closed

[SR-9447] Lazy operations from sequence extensions aren't lazy #51911

swift-ci opened this issue Dec 8, 2018 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 8, 2018

Previous ID SR-9447
Radar None
Original Reporter dbplunkett (JIRA User)
Type Bug
Status Closed
Resolution Invalid
Environment

Xcode 10.1 (10B61)

Swift 4.2.1

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee dbplunkett (JIRA)
Priority Medium

md5: 8c96a660fda32f886f256e4c39f74857

Issue Description:

Accessing `lazy` from an extension on any Sequence and performing any lazy operation on it results in the operation being performed non-lazily.

In the below example, performing `map` lazily from outside the instance behaves as expected, but calling a function defined in an extension on the instance's type that performs the same operation results in `transform` being applied to every element, i.e. non-lazily.

extension Array where Element == Int {
    func lazyMap(_ transform: (Int) -> Int) {
        lazy.map(transform)
    }
}
let transform: (Int) -> Int = {
    print($0)
    return $0
}
let a = [0, 1]
a.lazy.map(transform) // ✓ prints nothing
print("-")
a.lazyMap(transform) // ✗ prints `0` and `1`
@belkadan
Copy link
Contributor

Ooh, that's a weird one. Are we using the Self context to choose overloads even when not calling a method on a Self value? cc @moiseev, @xedin

@swift-ci
Copy link
Collaborator Author

Comment by David Plunkett (JIRA)

I did some more digging and realized it's because I'm not annotating the transform parameter as @escaping. Since lazily applied closures would naturally outlive the initial call, the non-escaping argument means that it's forced to choose the non-lazy version of `map`. I assume lazy sequences provide a non-lazy map because it was desired to provide a rethrowing `map`, which means it must be non-lazy. But one might assume there's no such thing as a non-lazy `map` on a lazy sequence, as I did.

@swift-ci
Copy link
Collaborator Author

Comment by David Plunkett (JIRA)

Since this seems like an API design detail, and not a bug, I'll close this.

@swift-ci
Copy link
Collaborator Author

Comment by David Plunkett (JIRA)

Actually, my guess about a rethrowing map being necessarily non-lazy seems wrong. I don't know why there's a non-lazy map, or why there appears to be no non-lazy version of `map` that rethrows. But I guess this is still API design territory, and not a bug?

@moiseev
Copy link
Mannequin

moiseev mannequin commented Dec 10, 2018

dbplunkett (JIRA User) consider the lazy map, accepting a throwing closure. Where will you actually put a `try` in this case? In case of an eager `map` that returns an array from a sequence, the answer is straightforward: you `try` right there where you call `map`. For a lazy – not so much. It is one of those unfortunate instances where behavior is different depending on `throw`-ingness of a closure.

@swift-ci
Copy link
Collaborator Author

Comment by David Plunkett (JIRA)

Ah, of course. Thanks for the explanation. I do wonder if lazy sequences should define eager operations at all then - forcing the user to turn it into a regular sequence before performing an eager operation.

@moiseev
Copy link
Mannequin

moiseev mannequin commented Dec 10, 2018

That is an interesting design question. But first and foremost, can you define what you mean by "lazy sequence"? Oh, and BTW, for the benefit of the community, how do you feel about moving this discussion over to the forums? Maybe into "Using Swift" category.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants