Here's the code:
class Test<T> {
func foo<S:SequenceType where S.Generator.Element == T>(par : S){
print("foo")
}
}
class TestInh : Test<Int> {
override func foo<S:SequenceType where S.Generator.Element == Int>(par : S) {
print("loo")
}
}
And it yells such error:
repl.swift:8:19: error: method does not override any method from its superclass
override func foo<S:SequenceType where S.Generator.Element == Int>(par : S) {
~~~~~~~~ ^
How could I override the method in super class Test<Int>
?
==================additional=======================
When it comes to the code blow.
class Test<T> {
func foo(par : T){
print("foo")
}
}
class TestInh : Test<Int> {
override func foo(par : Int) {
print("loo")
}
}
Everything works fine. Not knowing what happened on where
statement's appearing.
When inheriting a non-generic class from a generic one, you should:
override
with non-generic methods; butoverride
with generic ones.In both cases you can still call super
implementation of the said method.
Therefore, this should work:
class Base<T> {
func testGeneric<S: SequenceType where S.Generator.Element == T>(sequence: S) {
print("Base \(__FUNCTION__)(sequence: \(sequence.dynamicType))")
}
func testNongeneric(element: T) {
print("Base \(__FUNCTION__)(element: \(element.dynamicType))")
}
}
class Subclass: Base<Int> {
func testGeneric<S: SequenceType where S.Generator.Element == Int>(sequence: S) {
super.testGeneric(sequence)
print("Subclass \(__FUNCTION__)(sequence: \(sequence.dynamicType))")
}
override func testNongeneric(element: Int) {
super.testNongeneric(element)
print("Subclass \(__FUNCTION__)(element: \(element.dynamicType))")
}
}
Test:
let base = Base<Double>()
let subclass = Subclass()
base.testGeneric([]) // Prints: Base testGeneric(sequence: Array<Double>)
subclass.testGeneric([]) // Prints: Base testGeneric(sequence: Array<Int>)
// Subclass testGeneric(sequence: Array<Int>)
base.testNongeneric(0) // Prints: Base testNongeneric(element: Double)
subclass.testNongeneric(0) // Prints: Base testNongeneric(element: Int)
// Subclass testNongeneric(element: Int)
The reason you received the error is because the method signatures of the func
and the override func
are different:
func foo<S:SequenceType where S.Generator.Element == T>(par : S)
Is not the same as:
override func foo<S:SequenceType where S.Generator.Element == Int>(par : S)
If inheritance is important to you in this case then you will need to make your subclass a generic entity with a generic type declaration of T
, and amend your override func
as so:
class Test<T> {
func foo<S:SequenceType where S.Generator.Element == T>(par : S) {
print("foo")
}
}
class TestInh<T> : Test<T> {
override func foo<S:SequenceType where S.Generator.Element == T>(par : S) {
print("loo")
}
}
This should resolve your issue at hand. The reason this resolves is that the method signatures are now the same:
func foo<S:SequenceType where S.Generator.Element == T>(par : S)
Is the same as:
override func foo<S:SequenceType where S.Generator.Element == T>(par : S)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With