How to use a protocol to infer a generic type with constraints

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

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Hi Slava,

Thanks for your quick response!

I see. That's in line with what I can read from the Type Constraint Syntax
section in the official Swift book.

Satoshi

···

On Tue, May 2, 2017 at 1:59 AM, Slava Pestov <spestov@apple.com> wrote:

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@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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

Does anyone have an idea when this is going to be fixed? Or when support
will be implemented?
My app heavily uses collections of heterogenous plug-ins, so it's a real
pain in the neck to work around.

···

On Tue, May 2, 2017 at 2:08 AM, Satoshi Nakagawa via swift-users < swift-users@swift.org> wrote:

Hi Slava,

Thanks for your quick response!

I see. That's in line with what I can read from the Type Constraint Syntax
section in the official Swift book.

Satoshi

On Tue, May 2, 2017 at 1:59 AM, Slava Pestov <spestov@apple.com> wrote:

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@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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

My understanding is that allowing protocols to conform to themselves would be a *ton* of work. That’s not to say it’ll never happen, but the current behavior isn’t considered a bug, and I don’t think the feature is likely to happen without a proposal getting accepted on swift-evolution (it’s come up a few times before… IIRC it’s always been shot down because something like “yes, we’d all like it, but we only have so much time before the next release and there’s some much lower-hanging fruit that we should work on first”).

Feel free to start another thread about it… it’d get a +1 from me. I’d almost bet money though that it won’t happen until at least Swift 5 (unless it’s a surprise at WWDC or something).

- Dave Sweeris

···

On May 2, 2017, at 10:16 AM, Edward Connell via swift-users <swift-users@swift.org> wrote:

Does anyone have an idea when this is going to be fixed? Or when support will be implemented?
My app heavily uses collections of heterogenous plug-ins, so it's a real pain in the neck to work around.

Another +1 from me, a real pain

-- Howard.

···

On 3 May 2017, at 4:05 am, David Sweeris via swift-users <swift-users@swift.org> wrote:

On May 2, 2017, at 10:16 AM, Edward Connell via swift-users <swift-users@swift.org> wrote:

Does anyone have an idea when this is going to be fixed? Or when support will be implemented?
My app heavily uses collections of heterogenous plug-ins, so it's a real pain in the neck to work around.

My understanding is that allowing protocols to conform to themselves would be a *ton* of work. That’s not to say it’ll never happen, but the current behavior isn’t considered a bug, and I don’t think the feature is likely to happen without a proposal getting accepted on swift-evolution (it’s come up a few times before… IIRC it’s always been shot down because something like “yes, we’d all like it, but we only have so much time before the next release and there’s some much lower-hanging fruit that we should work on first”).

Feel free to start another thread about it… it’d get a +1 from me. I’d almost bet money though that it won’t happen until at least Swift 5 (unless it’s a surprise at WWDC or something).

- Dave Sweeris
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users