SwiftUI is partially an experiment to test those waters, I think. They're nice!
But there are other possibilities, if we can get a standard library boxing type.
struct Struct {
@Wrapped var value = "π"
}
Struct().$value { "π \($0)" }
Struct().$value.if(isSnackTime) { "π \($0)" }
@propertyWrapper struct Wrapped<Value> {
let wrappedValue: Value
var projectedValue: Self { self }
}
extension Wrapped {
typealias Transform = (Value) throws -> Value
func callAsFunction(transform: Transform) rethrows -> Value {
try transform(wrappedValue)
}
func `if`(_ condition: Bool, transform: Transform) rethrows -> Value {
condition
? try transform(wrappedValue)
: wrappedValue
}
}
β¦just make the $ do what the Β’ does here, and remove the need for parentheses.
prefix operator Β’
prefix func Β’<Value>(value: Value) -> Wrapped<Value> {
.init(wrappedValue: value)
}
(Β’"π") { "π \($0)" }
(Β’"π").if(isSnackTime) { "π \($0)" }