|
| 1 | +extension _DefaultInitializable { fileprivate static var placeholder: Self { Self() } } |
| 2 | +extension AdditiveArithmetic { fileprivate static var placeholder: Self { .zero } } |
| 3 | +extension ExpressibleByArrayLiteral { fileprivate static var placeholder: Self { [] } } |
| 4 | +extension ExpressibleByBooleanLiteral { fileprivate static var placeholder: Self { false } } |
| 5 | +extension ExpressibleByDictionaryLiteral { fileprivate static var placeholder: Self { [:] } } |
| 6 | +extension ExpressibleByFloatLiteral { fileprivate static var placeholder: Self { 0.0 } } |
| 7 | +extension ExpressibleByIntegerLiteral { fileprivate static var placeholder: Self { 0 } } |
| 8 | +extension ExpressibleByUnicodeScalarLiteral { fileprivate static var placeholder: Self { " " } } |
| 9 | +extension RangeReplaceableCollection { fileprivate static var placeholder: Self { Self() } } |
| 10 | + |
| 11 | +#if swift(>=5.7) |
| 12 | + |
| 13 | +private func _placeholder<Result>() -> Result? { |
| 14 | + switch Result.self { |
| 15 | + case let type as _DefaultInitializable.Type: return type.placeholder as? Result |
| 16 | + case is Void.Type: return () as? Result |
| 17 | + case let type as any RangeReplaceableCollection.Type: return type.placeholder as? Result |
| 18 | + case let type as any AdditiveArithmetic.Type: return type.placeholder as? Result |
| 19 | + case let type as any ExpressibleByArrayLiteral.Type: return type.placeholder as? Result |
| 20 | + case let type as any ExpressibleByBooleanLiteral.Type: return type.placeholder as? Result |
| 21 | + case let type as any ExpressibleByDictionaryLiteral.Type: return type.placeholder as? Result |
| 22 | + case let type as any ExpressibleByFloatLiteral.Type: return type.placeholder as? Result |
| 23 | + case let type as any ExpressibleByIntegerLiteral.Type: return type.placeholder as? Result |
| 24 | + case let type as any ExpressibleByUnicodeScalarLiteral.Type: return type.placeholder as? Result |
| 25 | + default: return nil |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +private func _rawRepresentable<Result>() -> Result? { |
| 30 | + func posiblePlaceholder<T: RawRepresentable>(for type: T.Type) -> T? { |
| 31 | + (_placeholder() as T.RawValue?).flatMap(T.init(rawValue:)) |
| 32 | + } |
| 33 | + |
| 34 | + return (Result.self as? any RawRepresentable.Type).flatMap { |
| 35 | + posiblePlaceholder(for: $0) as? Result |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +private func _caseIterable<Result>() -> Result? { |
| 40 | + func firstCase<T: CaseIterable>(for type: T.Type) -> Result? { |
| 41 | + T.allCases.first as? Result |
| 42 | + } |
| 43 | + |
| 44 | + return (Result.self as? any CaseIterable.Type).flatMap { |
| 45 | + firstCase(for: $0) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#else |
| 50 | + |
| 51 | +private func _placeholder<Result>() -> Result? { |
| 52 | + if let result = (Result.self as? _DefaultInitializable.Type)?.placeholder { |
| 53 | + return result as? Result |
| 54 | + } |
| 55 | + |
| 56 | + if Result.self == Void.self { |
| 57 | + return () as? Result |
| 58 | + } |
| 59 | + |
| 60 | + switch Witness<Result>.self { |
| 61 | + case let type as AnyRangeReplaceableCollection.Type: return type.placeholder as? Result |
| 62 | + case let type as AnyAdditiveArithmetic.Type: return type.placeholder as? Result |
| 63 | + case let type as AnyExpressibleByArrayLiteral.Type: return type.placeholder as? Result |
| 64 | + case let type as AnyExpressibleByBooleanLiteral.Type: return type.placeholder as? Result |
| 65 | + case let type as AnyExpressibleByDictionaryLiteral.Type: return type.placeholder as? Result |
| 66 | + case let type as AnyExpressibleByFloatLiteral.Type: return type.placeholder as? Result |
| 67 | + case let type as AnyExpressibleByIntegerLiteral.Type: return type.placeholder as? Result |
| 68 | + case let type as AnyExpressibleByUnicodeScalarLiteral.Type: return type.placeholder as? Result |
| 69 | + default: return nil |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +private func _rawRepresentable<Result>() -> Result? { |
| 74 | + (Witness<Result>.self as? AnyRawRepresentable.Type).flatMap { |
| 75 | + $0.possiblePlaceholder as? Result |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +private func _caseIterable<Result>() -> Result? { |
| 80 | + (Witness<Result>.self as? AnyCaseIterable.Type).flatMap { |
| 81 | + $0.firstCase as? Result |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +private enum Witness<Value> {} |
| 86 | +private protocol AnyAdditiveArithmetic { static var placeholder: Any { get } } |
| 87 | +extension Witness: AnyAdditiveArithmetic where Value: AdditiveArithmetic { |
| 88 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 89 | +} |
| 90 | + |
| 91 | +private protocol AnyExpressibleByArrayLiteral { static var placeholder: Any { get } } |
| 92 | +extension Witness: AnyExpressibleByArrayLiteral where Value: ExpressibleByArrayLiteral { |
| 93 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 94 | +} |
| 95 | + |
| 96 | +private protocol AnyExpressibleByBooleanLiteral { static var placeholder: Any { get } } |
| 97 | +extension Witness: AnyExpressibleByBooleanLiteral where Value: ExpressibleByBooleanLiteral { |
| 98 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 99 | +} |
| 100 | + |
| 101 | +private protocol AnyExpressibleByDictionaryLiteral { static var placeholder: Any { get } } |
| 102 | +extension Witness: AnyExpressibleByDictionaryLiteral where Value: ExpressibleByDictionaryLiteral { |
| 103 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 104 | +} |
| 105 | + |
| 106 | +private protocol AnyExpressibleByFloatLiteral { static var placeholder: Any { get } } |
| 107 | +extension Witness: AnyExpressibleByFloatLiteral where Value: ExpressibleByFloatLiteral { |
| 108 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 109 | +} |
| 110 | + |
| 111 | +private protocol AnyExpressibleByIntegerLiteral { static var placeholder: Any { get } } |
| 112 | +extension Witness: AnyExpressibleByIntegerLiteral where Value: ExpressibleByIntegerLiteral { |
| 113 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 114 | +} |
| 115 | + |
| 116 | +private protocol AnyExpressibleByUnicodeScalarLiteral { static var placeholder: Any { get } } |
| 117 | +extension Witness: AnyExpressibleByUnicodeScalarLiteral |
| 118 | +where Value: ExpressibleByUnicodeScalarLiteral { |
| 119 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 120 | +} |
| 121 | + |
| 122 | +private protocol AnyRangeReplaceableCollection { static var placeholder: Any { get } } |
| 123 | +extension Witness: AnyRangeReplaceableCollection where Value: RangeReplaceableCollection { |
| 124 | + fileprivate static var placeholder: Any { Value.placeholder } |
| 125 | +} |
| 126 | + |
| 127 | +private protocol AnyRawRepresentable { static var possiblePlaceholder: Any? { get } } |
| 128 | +extension Witness: AnyRawRepresentable where Value: RawRepresentable { |
| 129 | + fileprivate static var possiblePlaceholder: Any? { |
| 130 | + (_placeholder() as Value.RawValue?).flatMap(Value.init(rawValue:)) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +private protocol AnyCaseIterable { static var firstCase: Any? { get } } |
| 135 | +extension Witness: AnyCaseIterable where Value: CaseIterable { |
| 136 | + fileprivate static var firstCase: Any? { |
| 137 | + Value.allCases.first |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +#endif |
| 142 | + |
| 143 | +func _generatePlaceholder<Result>() -> Result? { |
| 144 | + if let result = _placeholder() as Result? { |
| 145 | + return result |
| 146 | + } |
| 147 | + |
| 148 | + if let result = _rawRepresentable() as Result? { |
| 149 | + return result |
| 150 | + } |
| 151 | + |
| 152 | + if let result = _caseIterable() as Result? { |
| 153 | + return result |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
0 commit comments