3

What I tried to do:

protocol HasElement {
    associatedtype ItemType
    func getElement() -> ItemType
    func setElement(element: ItemType)
}

class Element {}
class BarElement: Element {}

class Foo: NSObject, HasElement {
    typealias ItemType = Element
    func getElement() -> Element { ... }
    func setElement(element: Element) { ... }
}

class Bar: Foo {
    typealias ItemType = BarElement
    override func getElement() -> BarElement { ... } // This works.
    override func setElement(element: BarElement) { ... } // This fails.
}

The error is:

Method does not override any method from its superclass

If I try to use ItemType instead:

override func setElement(element: ItemType) { ... }  // Still fails.

The error is:

'ItemType' is ambiguous for type lookup in this context

Is there a way to make this work?

4
  • Have you tried setting it as Element as well? Should be less "ambiguous" Sep 27, 2016 at 23:36
  • Then I'd have to cast it to use it in the subclasses. The whole point of this exercise is to not have to cast the darn thing all the time.
    – i_am_jorf
    Sep 27, 2016 at 23:40
  • But yes, that does work.
    – i_am_jorf
    Sep 27, 2016 at 23:44
  • Ah I see, that is an interesting problem. Sep 28, 2016 at 0:14

3 Answers 3

2

Here's a way to do what you want:

protocol HasElement {
  associatedtype ItemType
  func getElement() -> ItemType
  func setElement(element: ItemType)
}

class Element {}
class BarElement: Element {}

class Foo: HasElement {
  // no need for typealias, the associated type is inferred
  func getElement() -> Element { return Element() }
  func setElement(element: Element) { }
}

class Bar: Foo {
  // no need for typealias, the associated type is inferred
  override func getElement() -> BarElement { return BarElement() }

  // hide the parent class method
  @available(*, unavailable, message: "Use setElement(element: BarElement)")
  override func setElement(element: Element) { }

  // comply with protocol in this class
  func setElement(element: BarElement) { }
}

// can't do this now:
let myElement = Element()
let myBar = Bar()
myBar.setElement(element: myElement) // Error: 'setElement(element: BarElement)' is unavailable: Use setElement(element: BarElement)
2
  • 1
    Interesting solution, although note that by overloading instead of overriding you lose dynamic dispatch, which may or may not be desirable. If you define myBar as let myBar : Foo = Bar(), then I would expect the setElement(element:) method from Foo to be called, rather than Bar's method. Although what's interesting is that when doing that, neither method seems to get called – until you remove the unavailable attribute.
    – Hamish
    Sep 28, 2016 at 10:12
  • 1
    I agree that it may not be the best solution. I think that inheritance might not be the way to go, instead a protocol-based approach might give better results.
    – user887210
    Sep 28, 2016 at 10:17
2

The problem here isn't associated types, it's that method inputs are contravariant. Therefore you cannot override a method that expects a given superclass instance input with a method that expects a subclass instance input.

In fact, you can simply boil your code down to:

class Element {}
class BarElement : Element {}

class Foo {
    func setElement(element: Element) { }
}

class Bar : Foo {
    // error: Method does not override any method from its superclass
    override func setElement(element: BarElement) { }
}

You simply cannot override an (Element) -> Void method with a (BarElement) -> Void method. The reasons for this should be fairly obvious if you consider what would happen if you created a Bar instance and upcast it to Foo. You're now able to pass an Element instance to a method that expects a BarElement instance, which is illegal.

The reason it works for your getElement method is that method outputs are covariant. Therefore overriding a () -> Element method with a () -> BarElement method is perfectly legal, as even if you upcast a Bar instance to Foo, the BarElement instance returned from getElement can be freely upcast to Element.

As for solutions, it rather depends on your exact use case. It may well be that what you're trying to do doesn't require inheritance at all, and instead you can just conform Foo and Bar to HasElement separately.

1
  • Yeah, fair enough.
    – i_am_jorf
    Sep 28, 2016 at 17:43
1

This works:

override func getElement() -> BarElement { ... }

using override keyword is when function signature does not change (BarElement is still Element). You copy and paste function from super class with same function name, same parameter name and same return value type.

But

override func setElement(element: BarElement) { ... }

fails because when you change parameter type (with same parameter name). This case is overloading not overriding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.