[swift-evolution] [Proposal draff] abstract classes and methods
Matthew Johnson
matthew at anandabits.com
Thu Jan 7 13:36:15 CST 2016
> On Jan 7, 2016, at 1:18 PM, Gwendal Roué <gwendal.roue at gmail.com> wrote:
>
>
>> Le 7 janv. 2016 à 20:01, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> a écrit :
>>
>>> The problem, though, is that abstract classes still have something that protocol do not, and this is the encapsulation of implementation details. Protocols, on the other side, are open in the wide, expose all their inner guts. The problem with convenience protocols that expose too much implementation details is that they make refactoring very difficult, if possible at all. Abstract classes don’t have this issue.
>>
>> That doesn’t have to be the case. In fact, if an enhancement came along that allowed protocols to have stored properties I think there will be a request to allow access control on those properties. If they aren’t required to have initial values some way to initialize them would also be necessary. This would allow pretty strong encapsulation.
>
> You clearly address the hiding of implementation details from the code that uses types adopting such a protocol. But I’m not sure those implementation details can be hidden from the adopting types themselves?
Yes, it would be possible to have `private` stored properties in a protocol that were only visible within the file declaring the protocol. Presumably they would be used by default implementations or `final` methods added in an extension in the same file.
I do not know whether Swift will go down the path of allowing this or not but it would be possible.
>
>> It is also possible that stored properties in protocols won’t be adopted or would be adopted without allowing for encapsulation. If that is the path that we follow then you make good points. I am just suggesting that we might want to wait and see before considering abstract classes. It might turn out that they really aren’t necessary.
>
>
> And we’ll continue fatalError("subclass must override") :-)
If we end up in that situation I would not be surprised to see a proposal for abstract classes. But I’d rather wait and see.
>
> Gwendal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160107/2432534c/attachment.html>
More information about the swift-evolution
mailing list