Last active
August 29, 2015 14:12
-
-
Save ChristianKienle/22bfa174c390e551d810 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class QueryResult {} // if I make this class public then the compiler says that generate has to be declared public. | |
// If I make generate public I get a different error... | |
// But QueryResult is a class which has to be public... it is part of my public API... | |
public class Row {} | |
class QueryResultGenerator : GeneratorType { | |
typealias Element = Row | |
func next() -> Element? { | |
return nil | |
} | |
} | |
extension QueryResult : SequenceType { | |
typealias GeneratorType = QueryResultGenerator | |
func generate() -> GeneratorType { | |
return QueryResultGenerator() | |
} | |
} | |
Making all things public does compile and It should be correct by a logical point of view. If you want to keep QueryResultGenerator private I think you'll need to create some wrapper class.
import Foundation
public class QueryResult {}
public class Row {}
public class QueryResultGenerator : GeneratorType {
public typealias Element = Row
public func next() -> Element? {
return nil
}
}
extension QueryResult : SequenceType {
public typealias GeneratorType = QueryResultGenerator
public func generate() -> GeneratorType {
return QueryResultGenerator()
}
}
Thanks man!
Your code works.
To answer your question:
I would like QueryResult
to be a public class and I would like to be able to use it in a for loop like this:
let r = QueryResult(...)
for row in r {
r.doSomething()
}
I am only able to do this if I make the generator public. You mentioned that:
If you want to keep QueryResultGenerator private I think you'll need to create some wrapper class.
QueryResultGenerator does not have to be private so all is good. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think that if you have a public class, the public methods must return only types that are public. As the class is public, it can be called from external code, that need to have access to all the types involved. It is your intention to create a public class or a private class?