Skip to content

Instantly share code, notes, and snippets.

@fitomad
Last active February 3, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/6b696bd496e1dcd5c919 to your computer and use it in GitHub Desktop.
Save fitomad/6b696bd496e1dcd5c919 to your computer and use it in GitHub Desktop.
//
// Swift version 2.2-dev (LLVM 3ebdbb2c7e, Clang f66c5bb67b, Swift 42591f7cba)
// Target: x86_64-unknown-linux-gnu
//
// [Swift IBM Sandbox](http://swiftlang.ng.bluemix.net/#/repl)
//
import Foundation
///
/// A little class
///
public class Message
{
public var text: String
public var sendDate: NSDate
public init(_ text: String)
{
self.text = text
self.sendDate = NSDate()
}
}
///
/// A little struct
///
public struct Response
{
public var text: String
public var responseDate: NSDate
public init(_ text: String)
{
self.text = text
self.responseDate = NSDate()
}
}
/**
Change `:` to `==` in *where* clause works fine
*/
extension CollectionType where Generator.Element: Message
{
func doSomeThingWithMessages() -> Void
{
print("There are \(self.count) message(s)")
}
}
/**
Change `==` to `:` in *where* clause cause
the following error
**type 'Generator.Element' constrained to non-protocol type 'Response'**
*/
extension CollectionType where Generator.Element == Response
{
func doSomeThingWithReponses() -> Void
{
print("There are \(self.count) response(s)")
}
}
//
// Go with example
//
let message: Message = Message("Where are you from?")
let response: Response = Response("I'm from Madrid")
let another_message: Message = Message("Great!")
let messages: [Message] = [ message, another_message ]
let responses: [Response] = [ response ]
messages.doSomeThingWithMessages()
//messages.doSomeThingWithReponses() // #Error
// Error: 'Response' is not convertible to 'Message'
responses.doSomeThingWithReponses()
//responses.doSomeThingWithMessages() // #Error
// Error: Response' is not a subtype of 'Message'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment