[swift-evolution] [Pitch] Nested extensions and stored properties
Víctor Pimentel Rodríguez
vpimentel at tuenti.com
Thu Mar 30 08:51:10 CDT 2017
On Thu, Mar 30, 2017 at 1:16 PM, Vladimir.S via swift-evolution
<swift-evolution at swift.org> wrote:
> So, both top level extensions and nested would have only 'fileprivate',
> 'internal' or 'public'. 'scoped' is not allowed for them(if we accept the
> above suggestions):
>
> class MyType { // internal
> public var a = 10 // internal(because of type's access level)
> var b = 20 // internal
> scoped var c = 30 // scoped for type and for nested extensions
>
> // default and max level is fileprivate, bounded by type's access level
> fileprivate extension MyProtoA {
> scoped var d = 10 // scoped for extension, inaccessible outside of it
>
> var e = 20 // fileprivate
> internal var f = 20 // fileprivate
> public var g = 30 // fileprivate
>
> func foo() { print(c, i, m) } // can access c, i, m
> }
>
> // default and max level is internal, bounded by type's access level
> extension MyProtoB {
> scoped var h = 40 // scoped for extension, inaccessible outside of it
>
> var i = 50 // internal
> internal var j = 60 // internal
> public var k = 70 // public->internal(type's access level)
> }
>
> // default and max level is public, bounded by type's access level
I think this is internal even if declared public.
> public extension MyProtoC {
> scoped var l = 80 // scoped for extension, inaccessible outside of it
>
> var m = 90 // public -> internal(type's access level)
> internal var n = 100 // internal
> public var o = 110 // public->internal(type's access level)
> }
> }
>
> Do you see any drawbacks in such design? (given that we disallow scoped
> access modifier at top level of file and for nested extensions in type)
To me it's very difficult to understand at first glance what's
happening in that code, or what is the intent for those modifiers. For
example, in my mind it takes way too much to compute the access level
for a variable inside a nested extension.
For me it's immensely useful that the compilers warns you when you
declare a subtype/property/method with an wider access modifier than
the current scope.
In general the whole proposal looks complicated, and for me it's hard
to imagine an use case for such needs.
It probably be because of my own limitations, but in my day to day and
when reviewing open source code, I crave for simpler access levels,
not for more granularity. The only friction that I see is having to
declare a method/property fileprivate instead of private because you
want to use from an extension in the same file.
--
Víctor Pimentel
More information about the swift-evolution
mailing list