Skip to content

Instantly share code, notes, and snippets.

@stevenkramer
Last active June 6, 2018 08:35
Show Gist options
  • Save stevenkramer/9b5afca1bbdbe17a7e1f9f05dd65360c to your computer and use it in GitHub Desktop.
Save stevenkramer/9b5afca1bbdbe17a7e1f9f05dd65360c to your computer and use it in GitHub Desktop.
// Feed into swiftc - && ./main to see Swift 4.1 and Swift 4.2 behave differently
// Swift 4.1 results are different for generic and specialized functions, which is probably incorrect
typealias TestedType = Any
func genericLookup<T>() -> T?
{
let lookupValue: TestedType? = nil
if let item = lookupValue as? T
{
return item
}
return nil
}
func specializedLookup() -> TestedType?
{
let lookupValue: TestedType? = nil
if let item = lookupValue as? TestedType
{
return item
}
return nil
}
func testGeneric()
{
let optional: Optional<TestedType> = genericLookup()
if case .none = optional
{
print("Generic Optional is .none")
}
else
{
print("Generic Optional is .some")
}
if let item: TestedType = genericLookup() {
print("Your Optional was bound to a \(String(describing: item)) of type \(type(of: item))")
} else {
print("This nil is a nil")
}
}
func testSpecialized()
{
let optional: Optional<TestedType> = specializedLookup()
if case .none = optional
{
print("Specialized Optional is .none")
}
else
{
print("Specialized Optional is .some")
}
if let item: TestedType = optional
{
print("Your Optional was bound to a \(String(describing: item)) of type \(type(of: item))")
} else {
print("This nil is a nil")
}
}
testGeneric()
testSpecialized()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment