Changing control flow is always a pitfall, and I don't think yield should do so without any hint what is (or might be) happening.
I don't have any good examples where the accessor should take special care for "failed" yields, so I prefer a simple (user perspective) model where control is always returned to the accessor, no matter what happens during the yield.
When (if) yield obtains other uses, we could adopt the guard let pattern:
modify {
print("Going to yield")
yield &someValue
print("Done - maybe an error happened, but I don't care")
}
...
for value in values {
guard yield &value else {
print("Client doesn't want elements anymore, so we stop yielding")
break
}
print("Moving to the next element...")
}
print("Done - maybe an error happened, but I don't care")
...
We do! If an error is thrown by the code that interacts with the yielded memory, then the cleanup must be forbidden from throwing. And that”s what defer does.
x will still have value semantics. In your example, when you create getModify2 this will cause the contents of getModify to be copied. This causes getModify.x to be copied as getModify2.x. This will mean the backing buffer of getModify.x will no longer be uniquely referenced. So when you modify it, CoW will trigger. The (immaterial, in this case) difference is there will be 2 references to it when it is yielded, rather than the previous 3 when using get/set. Think of this as similar to what happens when you make a copy of a struct containing a string, then mutate the original with a mutating func that modifies x.
So I decided to add a modify accessor to the TupleCollection@jrose shared on twitter here a bit ago.
It didn't end up exactly how I expected so I am wondering would it be reasonable to allow yield in a non-escaping closures that are part of coroutines?
I am fine with the use of defer for clean up as proposed in the pitch but think that this could potentially address the need of defer in a lot of cases making it a little simpler to use correctly.
Does it need to yield only once? I don’t quite understand the mechanics behind it, but it seems to me that when a function is trying to modify a value (via its modify accessor for example) it may need repeated access to the value’s contents anyway. And assuming yield only yields to a single control point (a single caller), this should be fine. Cleanup can happen in defer.
That, or I’m misunderstanding the issue. Does the closure return control back to modify multiple times? That could be weird...
I guess that ousts closures, then. Is there any possibility that one might be able to yield onto a DispatchQueue? Or would plain threadlocking be necessary?
I mean, DispatchQueue only ever executes a synced closure once, correct? Might we add some sort of annotation to hint to the compiler that certain closures will only execute once?
I think this is a really interesting pitch and would like to see it move forward.
About the discussion about yield and defer, I think it's alright as it is.
I like yield not only because it says what it does, but also because is the common name used to refer to coroutines.
I know this is not a topic about async but if we introduce coroutines at the language level I honestly would like to see the same keyword on all features, including async. (imo async is too specific)
The usage of defer is quite clear once you understand what's going on. Is not obvious for a newcomer but honestly this feature is a little advanced IMO. You have the normal set to start with, where you don't need all this knowledge. That said, if we find an elegant way of improving this I'm all in :)
Also not sure if this has been discussed (sorry, >200 posts! :) ) by I'm more worried about this than anything else on the pitch:
I noticed that Array's implementation of subscript uses only a _modify. Is there a reason why Array isn't taking advantage of the optimization of a set when performing straight assignments?
Late to the game but thanks, this is exactly what has been confusing me. I can't see the purpose of the coroutine concept here, when all you really do seems to be to allow callers to run a modification closure on your backing property.
I can understand that there are other reasons for introducing (semi-) co-routines, perhaps related to SwiftUI or (let's hope:) async/await, and perhaps I am missing something.
Is it, though? One could argue that by leaving such sharp edges around, we make it an advanced feature which is not accessible to newcomers.
Especially if this generalises to all coroutines. Suddenly coroutines themselves become an advanced feature because of how careful you need to be.
Given how the language has developed since this pitch, I’m more in favour of a yield statement with a mandatory cancellation handler, like this:
yield &myThing else {
// yield failed (was cancelled). Clean up.
// similar to guard, you must return from this block.
}
// yield was successful. Do post-processing.
It’s simple to understand the possible control flow when you have it written there, and difficult to understand (with potentially disastrous consequences) if you don’t. Even experienced developers appreciate when things are made obvious.
But we should really move forward with this. I keep forgetting it’s not actually part of the language yet.