If we had a protocol ExpressibleByClosureLiteral
that allowed your type to be initialized directly from a closure, such as this:
public protocol ExpressibleByClosureLiteral {
associatedtype ClosureLiteralInput
associatedtype ClosureLiteralOutput
init(closureLiteral: (ClosureLiteralInput) -> ClosureLiteralOutput)
}
Then we could make an UnboundedRange
type like so:
public struct UnboundedRange<Bound>: RangeExpression where Bound: Comparable {
public func contains(_ bound: Bound) -> Bool { true }
public func relative<C: Collection>(to collection: C) -> Range<Bound> where Bound == C.Index {
collection.startIndex..<collection.endIndex
}
}
Finally we could add the magic functionality we currently have like this:
extension UnboundedRange: ExpressibleByClosureLiteral {
enum ClosureLiteralInput {
public static prefix func ... (rhs: Self) -> Void { }
}
typealias ClosureLiteralOutput = Void
public init(closureLiteral: (ClosureLiteralInput) -> Void) {
self = Self()
}
}