Skip to content

Instantly share code, notes, and snippets.

@Gurdeep0602
Created July 20, 2016 12:11
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 Gurdeep0602/6e88cd3325f54eb497fe028532455a21 to your computer and use it in GitHub Desktop.
Save Gurdeep0602/6e88cd3325f54eb497fe028532455a21 to your computer and use it in GitHub Desktop.
/// Cast the argument to the infered function return type.
func autocast<T>(some: Any) -> T? {
return some as? T
}
protocol Foo {
static func foo() -> Self
}
class Vehicle: Foo {
class func foo() -> Self {
return autocast(Vehicle())!
}
}
class Tractor: Vehicle {
override class func foo() -> Self {
return autocast(Tractor())!
}
}
func typeName(some: Any) -> String {
return (some is Any.Type) ? "\(some)" : "\(some.dynamicType)"
}
let vehicle = Vehicle.foo()
let tractor = Tractor.foo()
print(typeName(vehicle)) // Vehicle
print(typeName(tractor)) // Tractor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment