Where to start if you want to extend `Any`

To be honest I never thought about an infix operator. Even if I personally would prefer .with for readability reasons I'm quite surprised that I never tried such approach already. In my code base I use a protocol which I conform all of my types to that requires that functionality.

Maybe we could also introduce a left assignment operator? That way this feature would be completely opt-in and not another .dynamicType thing.

However I wonder if we can make it work with optional chaining, is it a bug is the precedence not correct here?

precedencegroup LeftAssignmentPrecedence {
  associativity: left
  higherThan: BitwiseShiftPrecedence
}

infix operator <- : LeftAssignmentPrecedence

@discardableResult
public func <- <T>(lhs: T, rhs: (inout T) throws -> ()) rethrows -> T {
  var value = lhs
  try rhs(&value)
  return value
}

struct S {
  var i: Int
  var j: Float
}

let a = S(i: 7, j: 0)
let b = a <- { $0.i = 1 }

let test: (inout S) -> () = { $0.j = 2 }

let c: S? = b
let d = c? <- test // error: optional chain has no effect, expression already produces 'S?'
let e = b <- test

@Erica_Sadun what do you think about an infix operator instead?

2 Likes