Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocols need to use String(self) rather than String(Self) #2

Closed
rollandjb opened this issue Jan 23, 2016 · 6 comments
Closed

Protocols need to use String(self) rather than String(Self) #2

rollandjb opened this issue Jan 23, 2016 · 6 comments

Comments

@rollandjb
Copy link

The use of Self fails in the presence of subclassed cells because String(Self) refers to the class directly conforming to the protocol, while String(self) refers correctly to the class of the instance.

@AliSoftware
Copy link
Owner

@rollandjb Are you sure about this?

I just tested this in a playground, so it seems to work as expected even with Self:

protocol Reusable {
  static var reusableIdentifier: String { get }
}

extension Reusable {
  static var reusableIdentifier: String {
    return String(Self)
  }
}

class ParentView: Reusable {}
class ChildView: ParentView {}

let ri = ChildView.reusableIdentifier
print(ri) // prints "ChildView", as expected

@rollandjb
Copy link
Author

Yes I am, as the following playground snippet shows. Of course, my explanation may be wrong.

protocol Reusable {
    static var reusableIdentifier: String { get }
}

extension Reusable {
    static var reusableIdentifier: String {
        return String(Self)
    }
}

class Cell {
    required init() {}
}

class ParentView:Cell, Reusable {}
class ChildView: ParentView {}

let ri = ChildView.reusableIdentifier
print(ri) // prints "ChildView", as expected

class TableView {}

extension TableView {
    func dequeue<T: Cell where T: Reusable>() -> T {
        print(T.reusableIdentifier)

        return T()
    }
}

let tableView = TableView()

tableView.dequeue() as ChildView // prints ParentView for Self, ChildView for self

@AliSoftware
Copy link
Owner

Ok, talked to someone working on the Swift compiler and it appears that it's actually a bug

https://twitter.com/jckarter/status/691643221487693824

Not sure if the right solution is to use self though, probably is but before making that change I want to investigate more and especially ensure that it won't have side-effects. Like for some edge cases like when you customize the description or debugDescription of the class or the CustomMirror etc.

I definitely have to add Unit Tests to that pod anyway, might be a good occasion to do so.

@rollandjb
Copy link
Author

That's interesting. And of course you are right, customising description and debugDescription could cause trouble! In the mean time it works for me where I derive cells from a base cell.

Furthermore, my explanation was inaccurate!

@AliSoftware
Copy link
Owner

Bug reported on the Swift bug tracker: https://bugs.swift.org/browse/SR-617

Will try to add unit tests and see how we could workaround this (using self maybe as you suggested, but have to confirm with tests it works in all conditions first) soon. (Don't hesitate to make a PR to add some Unit Tests too, especially test cases matching your use case and demonstrating the issue)

@AliSoftware
Copy link
Owner

Closed by 5cc715c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants