Skip to content

Instantly share code, notes, and snippets.

@akisute
Created June 9, 2014 10:35
Show Gist options
  • Save akisute/8892ff05c4294ddbb5b9 to your computer and use it in GitHub Desktop.
Save akisute/8892ff05c4294ddbb5b9 to your computer and use it in GitHub Desktop.
import Foundation
class GrayShark {
var name:String
var howSwim:String
init(name:String, howSwim:String) {
self.name = name
self.howSwim = howSwim;
}
}
func letSharkSwim(shark:GrayShark) -> String {
return "\(shark.name) swims like '\(shark.howSwim)'."
}
// OK, "The Average Shark swims like Swim Swim Swim Lurk"
letSharkSwim(GrayShark(name:"The Average Shark", howSwim:"Swim Swim Swim Lurk"))
// Error
// Could not find an overload for '__conversion' that accepts the supplied arguments
//letSharkSwim(nil)
// Custom Conversion Example
extension NilType {
@conversion func __conversion() -> (GrayShark) {
return GrayShark(name:"Gray Shark", howSwim:"UNBELIEVABLE POWERRRRRRRR")
}
}
// OK, "Gray Shark swims like UNBELIEVABLE POWERRRRRRRR"
letSharkSwim(nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment