[swift-evolution] Protocol declaration inside a class

Slava Pestov spestov at apple.com
Thu Dec 10 22:27:04 CST 2015


Hi Victor,

> On Dec 10, 2015, at 2:25 PM, Виктор Шаманов via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Happy Friday everyone!
> 
> I wonder why the protocol declaration should be only at the file scope?
> Is this a design choice or limitation of the current implementation?

It is mostly the latter. As long as a protocol does not generic parameters from the outer type, there shouldn’t be any conceptual difficulties in allowing this, just implementation churn. It would make a good beginner project for someone who wants to dive deeper into Sema.

As for capturing generic parameters in nested types, we do want to make this possible for nested concrete types, but it needs a fair bit of Sema and SIL work. For protocols, one can imagine something like this:

class C<T> {
	protocol P {
		func f() -> T
	}

	struct S : P {
		func f() -> T { … }
	}
}

However I’m not sure what the implementation implications of this are.

Slava

> 
> I think, the ability of declaring protocols inside the other declarations would improve readability and code-organization.
> 
> For example, instead of:
> 
> protocol UITableViewDelegate {
> ...
> }
> 
> class UITableView {
>     var delegate: UITableViewDelegate?
> }
> 
> we would write:
> 
> class UITableView {
> 
>     protocol Delegate {
>     ...
>     }
> 
>     var delegate: Delegate?
> }
> 
> Thanks
>  _______________________________________________
> 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/20151210/8a494af0/attachment.html>


More information about the swift-evolution mailing list