Skip to content

Instantly share code, notes, and snippets.

@curtclifton
Last active August 29, 2015 14:26
Show Gist options
  • Save curtclifton/adc051008a5ebfdf484d to your computer and use it in GitHub Desktop.
Save curtclifton/adc051008a5ebfdf484d to your computer and use it in GitHub Desktop.
import Foundation
// Add a typealias for the child type so different implementors can be specific about the types of their children.
protocol NodeRepresentedObject {
typealias Child
var children: [Child]? {get}
}
protocol EmailMessage: NodeRepresentedObject {
}
// Add a typealias for the kind of messages. This lets us provide a default implementation for children, but requires mailboxes to specify a specific message type.
protocol Mailbox: NodeRepresentedObject {
typealias MessageKind: EmailMessage
var messages: [MessageKind] {get}
}
// A default implementation of children for Mailboxes.
extension Mailbox {
var children: [MessageKind]? {
return messages
}
}
// Finally a concrete message type. (Maybe we should provide the nil-returning implementation of children as a default?)
class IMAPMessage: EmailMessage {
var children: [IMAPMessage]? {
return nil
}
}
// And a concrete mailbox. This is more concrete than we might hope, since the message kind is hardcoded. We could use generics to avoid that…
class IMAPMailbox: Mailbox {
var messages = [IMAPMessage]()
}
// An alternative implementation of a concrete mailbox. This would be preferred if all the mailbox kinds had the same implementation and just varied based on content type.
class GenericMailbox<T: EmailMessage>: Mailbox {
var messages = [T]()
}
@curtclifton
Copy link
Author

This is a quick attempt at throwing type aliases and angle brackets at the problem Brent poses here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment