Skip to content

Instantly share code, notes, and snippets.

@chris-hatton
Last active February 8, 2016 04:28
Show Gist options
  • Save chris-hatton/7bc54c2eebea945fdfcf to your computer and use it in GitHub Desktop.
Save chris-hatton/7bc54c2eebea945fdfcf to your computer and use it in GitHub Desktop.
A test of inferred return types in Swift, both generic and overloaded nillable
import UIKit
class A : CustomDebugStringConvertible
{
let message: String
required init( message: String ) { self.message = message }
var debugDescription: String { return message }
}
class AA : A {}
class AB : A {}
func test<SomeType:A>() -> SomeType
{
return SomeType( message: "\(String(SomeType)) created through a non-nillable method invocation" )
}
func test<SomeType:A>() -> SomeType?
{
return SomeType( message: "\(String(SomeType)) created through a nillable method invocation" )
}
let first : AA? = test()
let second : AA = test()
let third : AB? = test()
let fourth : AB = test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment