Skip to content

Instantly share code, notes, and snippets.

@neonichu
Created December 29, 2014 20:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neonichu/e764ad902caead0176f9 to your computer and use it in GitHub Desktop.
Save neonichu/e764ad902caead0176f9 to your computer and use it in GitHub Desktop.
WTF Swift generics?
#!/usr/bin/env xcrun swift
import Foundation
class Base {
}
class Sub : Base {
}
func create<T : Base>() -> T {
println("param is: " + NSStringFromClass(T))
return T()
}
let o1: Sub = create()
println("result is: " + NSStringFromClass(o1.dynamicType))
@neonichu
Copy link
Author

$ ./generics.swift 
param is: generics.Sub
result is: generics.Base

The result should also be of type Sub, shouldn't it?

@orta
Copy link

orta commented Dec 29, 2014

maybe it isn't until it needs to be? the type is dynamic?

@neonichu
Copy link
Author

FWIW

let o2: Sub = Base()

correctly leads to

error: 'Base' is not convertible to 'Sub'

@neonichu
Copy link
Author

Submitted as a radar: http://www.openradar.me/19357437

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment