I was initially advertising for the new type, mostly because returning tuples like that is pretty yucky IMHO but it seems people are not too bothered by it. I would not mind the tuple version making a comeback.
FWIW I had been following this closely but realised the other day that most or all of what Iâm currently using AsyncStream for would probably be obviated by the new Observable concept, if/when that happens. In the context of an ObservableObject and its @Published properties, I often want a few more properties that I can easily listen to but that donât trigger objectWillChange, and ideally I like the interface to use Swift concurrency rather than Combine.
So, having had strong opinions about this API because I thought it would end up everywhere in my code, it looks likely that Iâll only be using it in certain cases the Observable implementation doesnât cover, and Iâm far less bothered about its exact shape.
My Use Case
class Speech: ObservableObject {
@Published var transcript = Transcript()
//
public var sessions: AsyncStream<Session> { sessionStream }
private let sessionStream: AsyncStream<Session>
private let sessionContinuation: AsyncStream<Session>.Continuation
//
public init() {
(sessionStream, sessionContinuation) = AsyncStream.create()
}
// ...
}
Thanks for the great feedback from everyone already. I am myself also not super tied to the new type approach so let's evaluate the feedback in the end.
I'd like to amend my +1 to say I also think a tuple (with or without a typealias) would be preferred over an entire new type. If we do use a typealias, I still would vote in favor of a name other than NewStream.
I canât judge what is best in this case but I definitely agree that the possibility of back deployment should NOT factor in the decision. Swift will be with us for a long time.
I prefer the tuple approach over an entirely new type. Type-aliasing it is fine, I think, but the âNewStreamâ name is a bit awkward since itâs not itself a stream. Something like âStreamCreationâ may be clunky, but it properly conveys that the âthingâ in question is an auxiliary, not a stream in its own right. Does anyone else have naming ideas?
Using a tuple seems more consistent with what we've previously done elsewhere in the standard library. If there's a docs tooling issue with documenting the different elements of a result tuple, that seems like something we should look into improving rather than having it affect library design. CC'ing @swift-documentation-workgroup
Iâm not much of a fan of tuples, but this seems like an obviously a proper use of them â a bundling of two values with no natural name and no pair-specific API. If we donât use tuples in a case like this, what are they even there for?
I don't mean separately from the method â in fact, that's one of the main problem with the struct return here, that it moves what's logically the documentation of a single function's return value to a different place. What I mean is that someone documenting the return value of a method that returns a tuple ought to feel like they have an acceptable way to talk about the different elements of the tuple. They can, of course, always just write stuff like "The first element of this tuple is the newly-created widget, which...", and maybe that's good enough and there's nothing else we need to do. But if there's an identifiable problem with that which is leading people to instead suggest returning a struct, I think we need to fix it so that people can continue to use tuples when otherwise appropriate.
what would be the alternative to doing as you suggest and just writing "the Nth element of this tuple"? i'm not sure how helpful symbollinking something like ``0`` would even be.
I'm not sure; I didn't raise the original documentation concern. I'm asking the docs workgroup to engage and figure out what the right thing to do here is, even if that's nothing.
I expect the tuple in this case would be labeled, though.