Skip to content

Instantly share code, notes, and snippets.

@werediver
Last active July 27, 2019 15:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save werediver/3909c8cce93aeb28e1b2 to your computer and use it in GitHub Desktop.
Save werediver/3909c8cce93aeb28e1b2 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