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

Joe DeCapo snoogansbc at gmail.com
Tue Dec 5 19:22:58 CST 2017


> On Dec 5, 2017, at 1:13 PM, Thorsten Seitz via swift-evolution <swift-evolution at swift.org> wrote:
> 
> let result = dynamic x.foo.bar  // will crash if foo or bar are not present
> 
> let result = dynamic? x.foo.bar // will return nil if foo or bar are not present
> 
> // will crash if foo or bar are not present
> let result = dynamic {
> 	let y = x.foo.bar
> 	let z = y.baz(42)
> 	return z.zork()
> }
> 
> // will return nil if foo or bar are not present
> let result = dynamic? {
> 	let y = x.foo.bar
> 	let z = y.baz(42)
> 	return z.zork()
> } 

As other people have pointed out, the behavior of something conforming to these protocols in no way implies that they crash if lookup fails. That is up to the implementer, and I assume most, if not all, will choose sane defaults (optional/throws) rather than just crashing.

But I think the main thing missing from this is where `x` came from. As I think Chris pointed out before, these values have to come from somewhere. If you have no idea what the type of the variable you're dealing with is and what its intended behavior might be, then I don't understand how you could even write this code in the first place.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171205/e8d976ca/attachment.html>


More information about the swift-evolution mailing list