Confused about implementing SequenceType

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Confused about implementing SequenceType

Jens Alfke
I defined a struct type that represents a set of Peer objects. (Using Set<Peer> would not be appropriate for reasons I’ll skip.) The internal implementation is a Dictionary mapping UUID strings to Peers. I want the set to be usable in a for…in… loop so I need to implement SequenceType. It’s straightforward except for the definition of the Generator typealias:

public struct PeerSet : SequenceType {
    private var byUUID = [String:Peer]()
    // ...
    public typealias Generator = ?????  // What goes here??
    public func generate() -> Generator {
        return byUUID.values.generate()
    }
}

OK, Generator has to be defined as “the same type that Dictionary.values.generate() returns”. But what exactly that type is, is not easy to work out; even armed with swiftdoc.org I was getting confused. Finally I solved the problem by replacing “?????” with “Int” and looking at the resulting error message:

PeerSet.swift:46:30: error: 'MapSequenceGenerator<DictionaryGenerator<String, Peer>, Peer>' is not convertible to 'Generator'
        return byUUID.values.generate()
               ~~~~~~~~~~~~~~^~~~~~~~~~

OK, so the actual type is MapSequenceGenerator<DictionaryGenerator<StringPeer>, Peer>. I replaced Int with that mouthful, and it works.

This seems a bit unintuitive and awkward, especially since the compiler already knows what the type should be (since it know how to name it in the error message.) Is there a clearer way to write this that isn’t as weird?

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/9B492C08-0B45-4F11-B649-49D184E8C737%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.