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

Chris Lattner sabre at nondot.org
Mon Nov 20 23:01:15 CST 2017

On Nov 20, 2017, at 7:15 PM, Michel Fortin via swift-evolution <swift-evolution at swift.org> wrote:
>>> As in `SupplementalDynamicMemberLookupProtocol` and `supplementalDynamicMember` for the subscript.
>> I’m totally open to suggestions for a better name, but I don’t see what “Supplemental” is adding here.  Recall that this protocol is compiler “magic” that is part of an implementation of a type, it isn’t really part of the user-exposed API of the type.  I wouldn’t expect a user to ever write:
>>    pyVal[dynamicMember: “foo”]
>> Even though they could.
> I'm not sure why you say it's not part of the user-exposed API. The type conforms to the protocol, and the protocol has this subscript, and there is nothing preventing someone from using it.

Yes, I understand that of course.

> And if for some reason you have the name of the member in a string variable and want to use it, you *have to* use it to get to the variable. So it'll definitely get used.

Not necessarily.  Nothing prevents a *specific* client from implementing a better accessor for their specific use, and I’d strongly encourage them to do so.  This is a compiler hook - just like ExpressibleByArrayLiteral exposes an ArrayLiteralElement associated type and init(arrayLiteral:..) initializer… even on Set! 

I have definitely seen user code using these, but that isn’t the intention.  The best solution to this is either to prevent them from being exported as API (my suggestion of something something like a “private conformance” implementation detail thing) or by simply marking these members with an attribute, so they don’t show up in code completion.

I do care about this sort of thing getting fixed, and I personally prefer the attribute+code completion change, but this is an existing problem in swift, orthogonal from this proposal.  If you’d like to see if fixed, then by all means, please bring this up and fix it.  It is more offensive that Int gets weird members like IntegerLiteralType and an integerLiteral initializer [[[which show up all the time…. :-( :-(   ]]] than the specific impact this proposal will have on a small set of narrow types people hardly interact with.

> Perhaps supplemental isn't the right word, but I wanted to convey that it supplements the Swift-defined methods and does not shadow them. For instance, in your PyVal protocol type there's a member `ownedPyObject` that won't be accessible with `dynamicMember` but will be dispatched through the protocol witness table.

Yes, I understand what you’re saying, but specific naming does matter.  I don’t think that ownedPyObject is in huge danger of conflicting with something that matters, but if it were, I’d suggest name mangling it to something even less likely to conflict, and I’d mark ownedPyObject with the same attribute to hide it from code completion, so people don’t see it every time they code complete on a PyVal.


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

More information about the swift-evolution mailing list