> Le 22 févr. 2017 à 16:31, Joanna Carter <[email protected]> a 
> écrit :
> 
> After much to-ing and fro-ing about scopes on Swift…

Now to classes :


Classes declared as private are actually just as visible as if they were 
declared to be fileprivate.

private class PrivateClass { }

internal class InternalClass
{
  private var pc: PrivateClass?
}

Understandable but, why have two visibilities that mean the same thing? Why can 
we declare a protocol or class as private? Convenience? Less typing?

Of course, we can extend a private class, as long as we declare the extension 
at file level :

private class PrivateClass { }

extension PrivateClass { }

However, if we try to nest a private class inside another (file)private type :

internal class InternalClass
{
  private class InternalPrivateClass { }
  
  private var ipc: InternalPrivateClass?
  
  extension InternalPrivateClass { } // error : Declaration is only valid at 
file scope
}

extension InternalClass.InternalPrivateClass { } // error : 
'InternalPrivateClass' is inaccessible due to 'private' protection level

… we now find that we have to differentiate between private and fileprivate in 
order to extend the nested type :


internal class InternalClass
{
  fileprivate class InternalPrivateClass { }
  
  private var ipc: InternalPrivateClass?
}

extension InternalClass.InternalPrivateClass
{
  
}

A private nested class can be truly private but, as soon as you want to extend 
that class in the same scope, we now have to switch to fileprivate because it 
is not possible to extend the nested class anywhere other than at file level. 
So, I have now had to raise the visibility of my private nested class just to 
allow me to extend it.

Once again, this is not the end of the world, just inconsistent. 

--
Joanna Carter
Carter Consulting

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to