Initializer inheritance doesn't work with generic class?


(Neil Faiman) #1

This compiles fine:

    class Sup {
        init(supDes: Int) {}
        convenience init(supCon: Int) { self.init(supDes: supCon) }
    }

    class Sub : Sup {
        convenience init(subCon1: Int) { self.init(supDes: subCon1) }
        convenience init(subCon2: Int) { self.init(supCon: subCon2) }
    }

But if I make the classes generic:

    class Sup<T> {
        init(supDes: T) {}
        convenience init(supCon: T) { self.init(supDes: supCon) }
    }

    class Sub<T> : Sup<T> {
        convenience init(subCon1: T) { self.init(supDes: subCon1) }
        convenience init(subCon2: T) { self.init(supCon: subCon2) }
    }

the compiler is doesn’t like it:

    error: argument labels '(supDes:)' do not match any available overloads
            convenience init(subCon1: T) { self.init(supDes: subCon1) }
                                                ^ ~~~~~~~~~~~~~~~~~
    note: overloads for 'Sub<T>.init' exist with these partially matching parameter lists: (subCon1: T), (subCon2: T)
            convenience init(subCon1: T) { self.init(supDes: subCon1) }
                                                ^
    error: argument labels '(supCon:)' do not match any available overloads
            convenience init(subCon2: T) { self.init(supCon: subCon2) }
                                                ^ ~~~~~~~~~~~~~~~~~
     note: overloads for 'Sub<T>.init' exist with these partially matching parameter lists: (subCon1: T), (subCon2: T)
            convenience init(subCon2: T) { self.init(supCon: subCon2) }
                                                ^

Expected behavior? Implementation limitation? Bug?

Thanks,

  Neil Faiman


(Neil Faiman) #2

Here is another, more blatant example of the failure to inherit initializers from a generic base class:

    class Concrete {
        init(value: Int) {}
    }

    class ConcreteSub : Concrete {
    }

    class Generic<T> {
        init(value: T) {}
    }

    class GenericSub : Generic<Int> {
    }

    let concrete = ConcreteSub(value: 1)
    let generic = GenericSub(value: 1)

error: 'GenericSub' cannot be constructed because it has no accessible initializers
    let generic = GenericSub(value: 1)
                  ^


(Joe Groff) #3

This is a known bug. I think Slava's currently working on fixing it.

···

On May 15, 2016, at 2:41 PM, Neil Faiman via swift-users <swift-users@swift.org> wrote:

Here is another, more blatant example of the failure to inherit initializers from a generic base class:

   class Concrete {
       init(value: Int) {}
   }

   class ConcreteSub : Concrete {
   }

   class Generic<T> {
       init(value: T) {}
   }

   class GenericSub : Generic<Int> {
   }

   let concrete = ConcreteSub(value: 1)
   let generic = GenericSub(value: 1)

error: 'GenericSub' cannot be constructed because it has no accessible initializers
   let generic = GenericSub(value: 1)
                 ^

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