Closure properties with argument labels

I've recently been exploring the pattern of usings structs with stored closures instead of protocols for types that are only realistically intended have a "live" and a "test" implementation. (Inspired by the fine folks at PointFree).

For example:

struct NetworkService {
    var sendRequest: (_ request: URLRequest) async throws -> Data
}

extension NetworkService {
    static func live(session: URLSession = .shared) -> NetworkService {
        NetworkService(sendRequest: { (request: URLRequest) in 
           // Do the thing...
        })
    }
}

The biggest hangup I've run into is that it's not currently possible to provide argument labels for closure properties. For example I couldn't cleanly change the signature of sendRequest(_:) to send(request: URLRequest). The issue only gets worse for closures that take multiple arguments.

A colleague of mine found this Swift Evolution proposal to remove the significants of function arguments from the type system circa Swift 3.

In the linked "Additional Commentary" a future direction is proposed where argument labels could be added to closures properties. Leaving space for that future without causing source breaking changes is explicitly called out as reason for requiring the _ before "cosmetic" closure labels in the current implementation.

I'd love to see the work outlined in the additional commentary picked up, but I don't have the compiler expertise to understand the scope of the change or the potential impact on things like ABI.

Is it still realistic to add this feature to Swift?

8 Likes

Forum link to Additional Commentary on SE–0111.

3 Likes

AFAIK this pitch is the most up-to-date work on this feature, but my PR with some initial support never got any traction and I haven't had the time to pick it up again recently.

If I have the time to work on this again I'll update that thread, but in the meantime anyone else should feel free to pick up where I left off.

6 Likes

It would be so nice to get those argument labels back! :heart:

4 Likes

It’d be great to have more parity between functions and closures, especially if you’re using that Point-Free technique. And expanding argument labels can only be a good thing: I consider them one of the defining elements of Swift, and they are sorely missed when I use other languages.

5 Likes