Protocol declaration inside a class


(Виктор Шаманов) #1

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?

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


(Slava Pestov) #2

Hi Victor,

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

···

On Dec 10, 2015, at 2:25 PM, Виктор Шаманов via swift-evolution <swift-evolution@swift.org> wrote:

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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution