[swift-evolution] [Pitch #2] Introduce User-defined "Dynamic Member Lookup" Types

Jose Cheyo Jimenez cheyo at masters3d.com
Sat Dec 2 21:58:09 CST 2017


Hi Chis, 

Thank you for pushing this forward.

My only comment is that on the declaration side it would be great to also have an attribute to communicate that compiler magic is happening.

Currently it is surprising that a regular looking protocol is providing me so much power.

Suggestions: 

@dynamic
struct PyVal : MemberLookupProtocol {...}

@dynamic
struct ParameterSummer : DynamicCallable {...}

// Error: This type needs the @dynamic attribute.
class ParamWinter : MyCustomCallableProtocolOrClassOrTypeAlias {...}

By requiring @dynamic (Or other attribute name), people can know that this is a compiler dynamic declaration and not just some random protocol whose name starts with Dynamic*. :)

@NSManagedObject is another example I like from Core Data.
https://useyourloaf.com/blog/core-data-code-generation/ <https://useyourloaf.com/blog/core-data-code-generation/>

- Cheyo



> On Nov 28, 2017, at 8:35 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Nov 27, 2017, at 6:21 PM, Brent Royal-Gordon <brent at architechies.com <mailto:brent at architechies.com>> wrote:
>> 
>>> On Nov 25, 2017, at 3:16 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> Just to talk to myself a bit here, but I’ve come to realize that the right design really is to have a simple empty marker protocol like this:
>> 
>> If you're reaching this point. why have a marker protocol at all? Why not treat `subscript(dynamicMember:)` specially on any type that has it, or have an `@dynamicMember subscript(_:)` attribute, or introduce an entire new `dynamicMember(_:)` declaration?
> 
> We’ve had a lot of discussions over the years about how to balance simplicity vs power, implicitness vs explicitness, intentionality vs accidental behavior, etc.  For example, in very early discussions about Swift generics, some folks where strong proponents of protocol conformance being fully implicit: satisfying all the requirements of a protocol meant that you conformed to it, even if you didn’t explicitly “inherit” from it.
> 
> This is obviously not the design we went with over the long term, and I’m glad we didn’t.  That said, if we did, then all of the “ExpressibleBy” protocols wouldn’t  need to exist: we’d probably just say that it was enough to implement the requirements to get the behavior and elide the protocol declaration entirely.
> 
> I think that DynamicMemberLookup requiring conformance is the same thing: it makes it explicit that the behavior is intentional, and it allows somewhat better error checking (if you conform to the protocol but don’t implement the (implicitly known) requirement, you DO get an error).  That said, this is just my opinion.  
> 
> Do you feel strongly enough about this that you’d like to make a strong argument for changing the behavior?
> 
> -Chris
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171202/45c5ee96/attachment.html>


More information about the swift-evolution mailing list