Last active
July 27, 2019 15:02
-
-
Save werediver/3909c8cce93aeb28e1b2 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
/// 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