[swift-evolution] [Discussion] Swift for Data Science / ML / Big Data analytics

Lukas Stabe lukas at stabe.de
Mon Oct 30 00:04:15 CDT 2017


> On 29. Oct 2017, at 21:34, Chris Lattner <clattner at nondot.org> wrote:
> 
> // Magic, allows “overloaded/sugared postfix ()”.
> protocol CustomCallable {
>  func call( …)
> }
> 
> The only tricky thing about this is the call part of things.  At least in the case of python, we want something like this:
> 
>   foo.bar(1, 2, a: x, b: y)
> 
> to turn into:
>  foo.dynamicMemberLookup(“bar”).call(1, 2, kwargs: [“a”:x, “b”:y])
> 
> We don’t want this to be a memberlookup of a value that has “bar” as a basename and “a:” and “b:” as parameter labels.

I don't know much about Swift internals, but since you can already access a function member without calling it, the parsing part of this sounds like it would be straightforward.

If CustomCallable looked something like this:

protocol CustomCallable {
    associatedtype Parameter
    associatedtype Result
    func call(_ parameterList: [(label: String?, value: Parameter)]) -> Result
}

implementations could decide how they want to handle named/unnamed parameters etc themselves.

This approach would currently come with some limitations though: No throwing (this could be addressed by a separate CustomThrowingCallable protocol), and – since currently non-nominal types can't adopt protocols – an implementation of CustomCallable could not accept, for example, both normal values and closures/tuples.

>> Are there other uses for such a thing?

DynamicMemberLookupable could enable libraries dealing with key-value data, like SwiftyJSON for example, to let users write myJson.someKey.nested instead of myJson["someKey"]["nested"]. I'm sure there are more uses like this that are not integrations with other languages.

— Lukas


More information about the swift-evolution mailing list