Skip to content

Instantly share code, notes, and snippets.

@ChristianKienle
Last active August 29, 2015 14:12
Show Gist options
  • Save ChristianKienle/22bfa174c390e551d810 to your computer and use it in GitHub Desktop.
Save ChristianKienle/22bfa174c390e551d810 to your computer and use it in GitHub Desktop.
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()
}
}
@mbigatti
Copy link

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()
    }

}

@ChristianKienle
Copy link
Author

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