Circling back to `with`

If we had a bind annotation in closure parameter type lists, as @DevAndArtist suggested, we would come very close to this, and additionally could use it in a number of other ways.

Swift has precedent for making it possible to create language-style features in libraries (see @autoclosure, trailing closure syntax and custom operator support), which is, in my opinion, a good way to approach things.

Coming back to your example, this is possible today:

func using<T>(_ val: T.Type, do closure: (T.Type) -> Void) {
    closure(val)
}

enum M {
    static let a: Int = 21
    static func double(_ n: Int) -> Int { return n * 2 }
}

var mutable: Int = 0

using(M.self) {
    mutable = $0.double($0.a)
}

Now if using could be declared as func using<T>(_ val: T.Type, do closure: (bind T.Type) -> Void), you could drop the $0, and we're quite close to your example except for the .self (which is something we probably want to get rid of at some point).

1 Like