> On 29. Oct 2017, at 21:34, Chris Lattner <clatt...@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
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to