[swift-users] How to use a protocol to infer a generic type with constraints
Slava Pestov
spestov at apple.com
Tue May 2 03:59:18 CDT 2017
Hi Satoshi,
Protocols do not conform to themselves. Only concrete types can conform to protocols in the current implementation of Swift.
Slava
> On May 2, 2017, at 1:57 AM, Satoshi Nakagawa via swift-users <swift-users at swift.org> wrote:
>
> Hi,
>
> I got a build error "Generic parameter 'T' could not be inferred" for the following code.
>
> Can anyone explain why we can't use a protocol to infer the generic type T?
>
> class Emitter {
> func emit<T: Emittable>() -> T {
> ...
> }
> }
>
> protocol Emittable {}
> protocol Subemittable: Emitable {}
>
> class ConcreteEmittable: Subemittable {}
>
> func testCode() {
> let emitter = Emitter()
>
> // Error: Generic parameter 'T' could not be inferred
> let _: Emittable = emitter.emit()
>
> // Error: Generic parameter 'T' could not be inferred
> let _: Subemittable = emitter.emit()
>
> // This works
> let _: ConcreteEmittable = emitter.emit()
> }
>
> Thanks,
> Satoshi
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
More information about the swift-users
mailing list