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

Ben Rimmington me at benrimmington.com
Mon Dec 4 07:42:30 CST 2017


> Chris Lattner wrote:
> 
> 	let np = Python.import("numpy")
> 	let b = np^.array^([6, 7, 8])
> 	let y =  np^.arange^(24)^.reshape^(2, 3, 4)
> 	
> 	let a = np^.ones^(3, dtype: np^.int32)
> 	let b = np^.linspace^(0, pi, 3)
> 	let c = a+^b
> 	let d = np^.exp^(c)

You could require `throws` on `subscript(dynamicMember:)` APIs.

"Throwing Properties and Subscripts proposal by brentdax"
<https://github.com/apple/swift-evolution/pull/218>

	let np = Python.import("numpy")
	let x = try! np.array([6, 7, 8])
	let y = try! np.arange(24).reshape(2, 3, 4)
	
	let a = try! np.ones(3, dtype: np.int32)
	let b = try! np.linspace(0, pi, 3)
	let c = try! a+b
	let d = try! np.exp(c)

If you allow dynamic member lookup on stdlib types:

	Double.pi.significant
	         ^
	ERROR: subscript can throw, but it is not marked with 'try'
	NOTE: did you mean 'significand'?

An implementation could fail by throwing an error or returning `nil`.
For example, JSValue/JSON member lookup could allow optional chaining.
But trying to invoke a non-callable JSValue could throw a TypeError.

-- Ben



More information about the swift-evolution mailing list