Last active
February 3, 2016 20:36
-
-
Save fitomad/6b696bd496e1dcd5c919 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// 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