更改泛型类中泛型方法的类型 [英] Change type of generic method in generic class

查看:131
本文介绍了更改泛型类中泛型方法的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑下列类别:

  //模型:
class A {}
class B:A {}

//解析器:
class AbstractParser< T> {}
class ParserB< T:B> ;: AbstractParser< T> {}

//服务:
class AbstractService< T> {
func parser() - > AbstractParser< T> {
fatalError(此方法必须被覆盖)
}
}
class ServiceA< T:A> ;: AbstractService< T> {
}
class ServiceB< T:B>:ServiceA< T> {
private let _parser = ParserB()

覆盖func parser() - > ParserB< B个{
return _parser
}
}

一个错误方法没有从它的超类覆盖任何方法在覆盖解析器函数。我可以通过更改

  class ServiceB< T:B>:ServiceA< T> 

  class ServiceB< T:B>:ServiceA< B> 

但这会打破这个问题的解决方案:泛型类中的变量获取错误的类型

有什么解决方法吗?



编辑

感谢Kenneth Bruno,你的方法起作用,但它又会导致类型的另一个错误。



我添加class C

  class C {
var item = B()
}

和一个简单的方法来 ServiceB :

  func doSomething (){
var entities = [T]()
let c = C()
entities.append(c.item)
}

$ 这会导致错误:无法用类型为'(B)'的参数列表调用'append'方法。 code>。看来编译器不能理解 B T 是一回事吗?



另请注意,我无法定义 var entities = [B](),因为我需要将此数组传递给另一个函数 AbstractService 方法。

解决方案

使用泛型类型而不是特定类型,那么方法签名将匹配以覆盖该函数。

  class ServiceB< T: B>:ServiceA< T> {
private let _parser = ParserB< T>()

覆盖func parser() - > ParserB< T> {
return _parser
}
}


Please consider the following classes:

// Models:
class A {}
class B: A { }

// Parsers:
class AbstractParser<T> {}
class ParserB<T: B>: AbstractParser<T> {}

// Services:
class AbstractService<T> {
    func parser() -> AbstractParser<T> {
        fatalError("This method must be overridden")
    }
}
class ServiceA<T: A>: AbstractService<T> {
}
class ServiceB<T: B>: ServiceA<T> {
    private let _parser = ParserB()

    override func parser() -> ParserB<B> {
        return _parser
    }
}

I'm getting an error Method doesn not override any method from it's superclasses at overriden parser function. I could easily fix this by changing

class ServiceB<T: B>: ServiceA<T>

to

class ServiceB<T: B>: ServiceA<B>

but this will break a solution from this question: A variable in generic class gets wrong type

Is there any workaround for this?

EDIT

Thanks, Kenneth Bruno, your approach works, but it again leads to another error with types.

I add class C:

class C {
    var item = B()
}

and a simple method to ServiceB:

    func doSomething() {
        var entities = [T]()
        let c = C()
        entities.append(c.item)
    }

This causes error: Cannot invoke 'append' method with an argument list of type '(B)'. It seems the compiler can't understand that B and T are the same thing?

Also please note that I can't define var entities = [B](), as I need to pass this array to another function in AbstractService method.

解决方案

Just as in your other question you need to use the generic type instead of a specific type, then the method signatures will match to override the function.

class ServiceB<T: B>: ServiceA<T> {
  private let _parser = ParserB<T>()

  override func parser() -> ParserB<T> {
    return _parser
  }
}

这篇关于更改泛型类中泛型方法的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆