First of all, just a note.
If the generic type giveGeneric just T , then it can be anything (String, Int, ...). So how to react in this case giveObject() ?
I mean, if you write:
let word : String = giveGeneric()
internally, your generic function calls something like:
let result : String = giveObject()
My decision
I declared the protocol as follows:
protocol MyObject { init() }
Then I made your 2 classes conform to the protocol
class obA: Printable, MyObject { var description: String { get { return "obA" } } required init() {} } class obB: Printable, MyObject { var description: String { get { return "obB" } } required init() {} }
Finally, I can write this
func giveGeneric<T:MyObject>() -> T { return T() }
Now I can use it:
let a1 : obA = giveGeneric() let b1 : obB = giveGeneric()
You decided that this is the solution you were looking for, or just a workaround.
source share