[swift-evolution] [Pitch #2] Introduce user-defined dynamically "callable" types

Ben Rimmington me at benrimmington.com
Thu Nov 23 12:30:30 CST 2017


DynamicCallable.md
<https://gist.github.com/lattner/a6257f425f55fe39fd6ac7a2354d693d>

DynamicMemberLookup.md
<https://gist.github.com/lattner/b016e1cf86c43732c8d82f90e5ae5438>

JavaScriptCore.JSValue
<https://developer.apple.com/documentation/javascriptcore/jsvalue>

If the JSValue class implements these protocols, 
how will it choose to invoke itself?

1. `call(withArguments:)`
2. `construct(withArguments:)`
3. `invokeMethod(_:withArguments:)`

import Foundation
import JavaScriptCore

let context = JSContext()!

let dateClass = context.objectForKeyedSubscript("Date")!
assert(dateClass.isObject)

// 1. `const dateString = Date()`
let dateString = dateClass.call(withArguments: [])!
assert(dateString.isString)

// 2. `const dateObject = new Date()`
let dateObject = dateClass.construct(withArguments: [])!
assert(dateObject.isObject)

// 3a. `const dateNumberA = Date.now()`
let dateNumberA = dateClass.invokeMethod("now", withArguments: [])!
assert(dateNumberA.isNumber)

// 3b. `const dateNumberB = dateObject.valueOf()`
let dateNumberB = dateObject.invokeMethod("valueOf", withArguments: [])!
assert(dateNumberB.isNumber)

-- Ben



More information about the swift-evolution mailing list