[swift-evolution] Proposal: Introduce User-defined "Dynamic Member Lookup" Types

Ben Rimmington me at benrimmington.com
Tue Nov 28 18:15:35 CST 2017


> On 28 Nov 2017, at 22:06, Jean-Daniel <mailing at xenonium.com> wrote:
> 
> While I perfectly understand why subscript is the best way to represent dynamic property, I don’t get how it help to abuse it to implement dynamic methods. Methods are not l-value, so why do you want to use a subscript for them ?

First, I tried overloading `subscript(dynamicMember:)` by return type.

subscript(dynamicMember name: String) -> (_ arguments: Any...) -> JSValue
subscript(dynamicMember name: String) -> JSValue

It allows for method calls without the DynamicCallable proposal.

context[dynamicMember: "Math"][dynamicMember: "pow"](2, 53)
//-> 9007199254740992

context[dynamicMember: "Math"][dynamicMember: "PI"] as JSValue
//-> 3.1415926535897931

context[dynamicMember: "Math"][dynamicMember: "PI"]
//ERROR: Ambiguous use of 'subscript(dynamicMember:)'

This evolved into the differently labelled subscripts,
to avoid having to disambiguate by return type.

But you're right, a function might be better than a subscript.

-- Ben



More information about the swift-evolution mailing list