[swift-evolution] [Pitch] consistent public access modifiers

Adrian Zubarev adrian.zubarev at devandartist.com
Wed Feb 15 12:15:33 CST 2017


I’ll try again to make it a little bit more simpler.

Assume we want to implement my feature to support document["string_literal", stringInstance, integerInstance, 42] where the first parameter type is always a String but the types follow afterward are either a String or an Int (literals should be supported too).

// Module A
// Today only `public` is allowed on protocols.
// `public protocol` means almost the same as `open class`.
// One is 'you can conform to it outside module A'
// the other is 'you can subclass to it outside module A'

// First idea
public SubscriptParameterType {}
extension Int : SubscriptParameterType {}
extension String : SubscriptParameterType {}

extension Document {
     
    public subscript(firstKey: String, parameters: SubscriptParameterType...) -> Value? {  
     
        // CAST any instance of     `SubscriptParameterType` to Int or String
    }
}

// Module B
extension NSObject : SubscriptParameterType {}

// Lets try breaking module A
document["test", NSObject()] // Runtime error, because something unexpected happened in module A

// The reason is we let the client (module B) the ability to conform to our type without any requirements.

// To solve that issue we need TODAY a requirement on our protocol `SubscriptParameterType` (see my previous post)
// I already mentioned that the workaround creates unnecessary boilerplate in my library.

// We could clean everything up iff we had consistent `public` vs. `open` behavior. I will use `pulic-but-not-open` and `open` annotation now.

pulic-but-not-open SubscriptParameterType {}
extension Int : SubscriptParameterType {}
extension String : SubscriptParameterType {}

// Module B
extension NSObject : SubscriptParameterType {} // Error, `SubscriptParameterType` is closed and cannot be conformed to outside module A!!
This feature is ridiculously powerful.



-- 
Adrian Zubarev
Sent with Airmail

Am 15. Februar 2017 um 18:57:50, Rien via swift-evolution (swift-evolution at swift.org) schrieb:


> On 15 Feb 2017, at 17:45, Matthew Johnson <matthew at anandabits.com> wrote:
>  
>  
>> On Feb 15, 2017, at 10:35 AM, Rien <Rien at Balancingrock.nl> wrote:
>>  
>>>  
>>> On 15 Feb 2017, at 17:02, Matthew Johnson <matthew at anandabits.com> wrote:
>>>  
>>>>  
>>>> On Feb 15, 2017, at 9:59 AM, Rien <Rien at Balancingrock.nl> wrote:
>>>>  
>>>>>  
>>>>> On 15 Feb 2017, at 16:45, Matthew Johnson <matthew at anandabits.com> wrote:
>>>>>  
>>>>>>  
>>>>>> On Feb 15, 2017, at 9:35 AM, Rien <Rien at Balancingrock.nl> wrote:
>>>>>>  
>>>>>>  
>>>>>>> On 15 Feb 2017, at 16:11, Matthew Johnson via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>>>  
>>>>>>>  
>>>>>>>> On Feb 15, 2017, at 5:59 AM, Jeremy Pereira via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>>>>  
>>>>>>>>  
>>>>>>>>> On 15 Feb 2017, at 11:11, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>>>>>>>>  
>>>>>>>>>  
>>>>>>>>> Our philosophy in general, however, is to default to the behavior which preserves the most flexibility for the library designer.
>>>>>>>>  
>>>>>>>> Actually, I thought the philosophy was to preserver type safety. When did that change?
>>>>>>>>  
>>>>>>>> Also, when was the library designer prioritised ahead of the application developer?
>>>>>>>>  
>>>>>>>>  
>>>>>>>>> Both open and non-open classes are common, but we chose to give non-open classes the `public` keyword because that's the flexibility-preserving option.
>>>>>>>>  
>>>>>>>> No it isn’t, it’s the flexibility restricting option. The consumer of an open class can subclass it. The consumer of a public class cannot subclass it. How is the second more flexible than the first?
>>>>>>>  
>>>>>>> It reduces complexity for the library author by allowing them to opt-out of the complexity involved in supporting unknown, user-defined subclasses. It is important to allow libraries to have this flexibility. They are free to declare a class `open` if they want to allow subclassing. It’s even possibly for a library to declare all classes `open` if it wishes to do so. But *requiring* that would reduce the design space libraries are allowed to explore and / or introduce fragility by moving the subclass restriction to a comment.
>>>>>>>  
>>>>>>  
>>>>>> Why would a library author want to prohibit subclasses?
>>>>>> A library user can always wrap the class and subclass the wrapper.
>>>>>  
>>>>> This is composition, not inheritance. The most important difference is that a wrapper cannot override methods, it can only wrap and / or forward them. This means that when the superclass calls a method on `self` that method *always* invokes its version of that method rather than a subclass override. This is a very important difference.
>>>>>  
>>>>  
>>>> Agreed, however that does not answer the question why would a library developer want to disallow subclassing?
>>>> I do not see a use case for that. I.e. a feature that cannot be implemented without it. (without “open”)
>>>  
>>> The feature it enables is more robust libraries and the ability for library authors to better reason about their code. You may not find this benefit enough to be worth a language feature, but many of us do.
>>  
>> You start of with a claim “more robust libraries”.
>> I would really like to know the “how” of that. How does it make a library more robust?
>>  
>> I do write libraries myself, and if there is something I am missing, I very much would like to know.
>  
> This topic was well explored during the discussion and review of the proposal that introduced `open`. If you would really like to know I suggest you take some time to read through that discussion.

I did (proposal 117), while it was mentioned a few times “if not carefully designed for subclassing” there is no example on how open vs public prevents anything.

Mind you, it could be argued that the default of “internal” instead of “open” can prevent something (though I still don’t know what). But adding “open” does imo not prevent anything.

Besides: a monkey with a tool is still a monkey.

I.e. devs that now start using “open” vs “public” because it all is a hassle to figure out what should be “internal”, “public” or “open” will still make the same errors (which ones?) as before.
Thus while there is a lot of bad code around (partly mine!) that does not disappear because the default is not “not subclassable”.

(PS: I am still trying to get my head around Adrian’s example)

Regards,
Rien.

>  
>>  
>> Regards,
>> Rien.
>>  
>>>  
>>>>  
>>>> Rien.
>>>>  
>>>>>>  
>>>>>> There are cases where subclassing does not make sense. And thus preventing subclasses adds information for those users that don’t RTFM. But that imo is not worth the impact extra complexity places on all other users.
>>>>>>  
>>>>>> Rien.
>>>>>>  
>>>>>>>>  
>>>>>>>>  
>>>>>>>> _______________________________________________
>>>>>>>> swift-evolution mailing list
>>>>>>>> swift-evolution at swift.org
>>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>>>>>  
>>>>>>> _______________________________________________
>>>>>>> swift-evolution mailing list
>>>>>>> swift-evolution at swift.org
>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>  

_______________________________________________
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/20170215/6b06884b/attachment.html>


More information about the swift-evolution mailing list