An explicit downcast of either the variable:
let short = SimpleTrain<T>() as ShortType
or the return value:
return short as ShortType
solves the problem.
Update
When I was answering to the question, I wondered myself how the Train
protocol can be used as a return type, being it typealiased.
Look at this code:
protocol ProtocolWithNoAlias {
}
protocol ProtocolWithAlias {
typealias AliasType
}
var x: ProtocolWithNoAlias?
var y: ProtocolWithAlias?
The last line is reported as a compilation error:
Protocol 'ProtocolWithAlias' can only be used as a generic constraint because it has Self os associated type requirements
which means you cannot use ProtocolWithAlias
as a concrete type, which in turn means you cannot declare variables having ProtocolWithAlias
type, and consequently it doesn't make sense to define function returning it.
I cannot find any mention about that in the official documentation, but I am sure I read somewhere, I just don't remember where.
Conclusion: I interpret the error you're having troubles with:
SimpleTrain<T> is not convertible to 'ShortType'
as a direct consequence of the protocol being typealiased.
Note that this is my personal opinion, since I am unable at this moment to prove it besides that code snippet testing protocol with and without aliases.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…