Created
July 20, 2016 12:11
-
-
Save Gurdeep0602/6e88cd3325f54eb497fe028532455a21 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// 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