[swift-evolution] [Proposal] Explicit Synthetic Behaviour
swift-evolution at haravikk.me
Thu Sep 7 05:20:46 CDT 2017
> On 7 Sep 2017, at 00:11, Brent Royal-Gordon <brent at architechies.com> wrote:
>> On Sep 5, 2017, at 1:02 PM, Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> This proposal idea is essentially for a new attribute @synthetic (name is up for debate). This attribute is required for any default implementation that includes reflective type compiler magic, use of the reflection API against `self` or, in future, any native Swift macros within the method (possibly limited to specific features, will depend on the macro language and its capabilities).
> "Use of the reflection API against `self`"? `String(describing:)` and `String(reflecting:)` sometimes do that.
> I see zero justification for having @synthetic cover all of these random things, but not ordinary default implementations—they have the same amount of dangerous implicitness.
Actually they don't; the problem here is that through reflection you are accessing and manipulating concrete types. A non-reflective default implementation only has access to what the protocol itself has defined. The synthetic alternatives are instead diving into parts of a concrete type that may have nothing to do with the protocol at all, and must therefore make assumptions that cannot be guaranteed to be correct, this is what makes them dangerous.
> On 6 Sep 2017, at 23:43, Nevin Brackett-Rozinsky <nevin.brackettrozinsky at gmail.com> wrote:
>> On Wed, Sep 6, 2017 at 5:42 PM, Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> the issue I'm trying to raise is that when those, and similar features, are used in synthesised behaviour (default implementations based upon the concrete type), that these behaviours should be opted into explicitly, otherwise they open up potential for all kinds of bugs, even when the assumptions being made about the concrete type are simple such as in the case for Equatable/Hashable. There's just too much potential for this kind of reflective protocol implementation to overreach; to me it feels very much like going into a restaurant and the waiter coming across and force-feeding me something I don't want instead of taking my order.
> I might suggest that instead it is like you have gone into a pizza shop and said, “I’d like a large veggie pizza please.” And they made you a pizza with their standard dough and their standard sauce and their standard cheese and their standard selection of vegetables.
Actually I don't think that's quite it either; to strain the analogy even further, I'd say it's more like going into a pizza shop and saying "I'd like a pizza" and the staff looking at you and deciding you look like a vegetarian and giving you a vegetarian pizza.
The crux of the issue here are the assumptions that are being made; for a standard default implementation there are no assumptions, because you're operating on the basis of methods and properties that you yourself have defined as the protocol creator that, where properly implemented, have precisely defined requirements, behaviours etc. When you're doing it with some form of compile-time or run-time reflection however you're messing around with parts of a concrete type that the protocol itself doesn't actually know anything about with any certainty.
It's the same reason that run-time reflection isn't something you should ever want to do, because while it might work with all of the types you test directly, if it supports arbitrary types (which it'd need to, or there'd be no point to reflecting) then there's always the risk of encountering a type where some part of it doesn't match the assumptions that you've made. Of course there are use-cases where this may not matter, e.g- if you're just dumping data from a type and don't really care if you're storing stuff that isn't important, but in other cases such as Equatable and Hashable it can make a difference, as it can drastically affect behaviour when those assumptions fail.
> On 7 Sep 2017, at 05:00, Andrew Thompson <mrwerdo331 at me.com> wrote:
> Perhaps we could find a solution by meeting in the middle. Introduce a compiler flag that will disable the automatic synthesis and revert to the old behaviour (i.e. swiftc main.swift —disable-automatic-synthesis )
Thanks for the suggestion, but that's a bit too all-or-nothing; I realise I might seem very negative but to be clear, I do want these synthesised features, I just don't want them in their current form. What I want is for them to be explicitly opted into where I, as the developer, need them to be, so that I'm in absolute, unambiguous control of when and where they are used.
I do absolutely support features that eliminate tedious boiler-plate, I just think that doing so implicitly in invasive ways is not the right way to do it.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution