2021年2月5日星期五

How to return an empty SubSequence in a Collection extension

I am creating an extension to Collection which returns a SubSequence. However, if the function's condition isn't valid I want to return an empty SubSequence instead of nil.

How can I return an empty SubSequence in an extension to Collection?

// basic example:  extension Collection {       func myFunction(condition: Bool) -> SubSequence {        guard condition else {            return *EmptySubSequence*        }        ...     }    }  

This would be very easy if I just did an extension on a concrete type, but before I limit my extension to a specific type, I would like to know if it can apply to Collection itself.

The best I've found is prefix(0) will return a SubSequence of length 0.

https://stackoverflow.com/questions/66073174/how-to-return-an-empty-subsequence-in-a-collection-extension February 06, 2021 at 10:59AM

没有评论:

发表评论