Swift:如何在变量中保存泛型类型的任何可能实例 [英] Swift: How to hold any possible instance of a generic type in a variable
问题描述
public struct HolderOfWrappers
{
let anyWrappedItem:MyResource< Any>
}
公共结构MyResource< A>
{
let wrappedItem:A
let convert:String - > A
}
func holdResource< A>(资源:MyResource< A>) - > HolderOfWrappers
{
//错误在这一行,A不是任何...
让wrapHolder:HolderOfWrappers = HolderOfWrappers(资源)
返回wrapHolder
}
现在,这段代码会在最后一个 holdResource
方法,我试图构建 HolderOfWrappers
:
无法将表达式的类型'MyResource< A>'转换为类型'(anyWrappedItem:MyResource< Any>)'
这是可以理解的,因为代码表明HolderOfWrappers只能容纳为任何类型构建的MyResource,而不是任何可能的类型。我对 HolderOfWrappers
的真正看法是: > public struct HolderOfWrappers
{
let anyWrappedItem:MyResource<>
}
甚至 MyResource <*>
- 我想用这段代码说,我想要一个可以容纳任何类型MyResource的变量。如果我尝试使用任何一种语法,我都会得到一个编译器错误,它期望一个类型。
我可以只有 anyWrappedItem
by类型任何
,但是您将丢失类型信息以供将来使用。我也不希望HolderOfWrapper是通用的(因为后来我只是有同样的问题)。
这几乎就像我试图将泛型类型视为一个协议为 anyWrappedItem
存储变量,这将不会用于其他原因...
我认为你可以通过在你的 HolderOfWrappers
init
方法中添加一个通用参数。基本上, init
方法只是使用资源$ c $生成一个新的
MyResource
c>你提供的是这样的:
public struct HolderOfWrappers {
let anyWrappedItem:MyResource<任何>
public init< A>(resource:MyResource< A>){
self.anyWrappedItem = MyResource(wrappedItem:resource.wrappedItem,convert:resource.convert)
}
}
我认为那样会做你想做的。我不知道这是否会因为你正在初始化一个全新的 MyResource
而不是只复制一个而变慢。
<无论如何,它使得
HolderOfWrappers
本身不是通用的,并且将 anyWrappedItem
填充为 MyResource< Any>
,它保存与您传入的资源
相同的值。 The distillation of what I am trying to do is this:
public struct HolderOfWrappers
{
let anyWrappedItem: MyResource<Any>
}
public struct MyResource<A>
{
let wrappedItem : A
let convert: String -> A
}
func holdResource<A>( resource: MyResource<A> ) -> HolderOfWrappers
{
// Error on this line, A is not Any...
let wrapHolder : HolderOfWrappers = HolderOfWrappers( resource )
return wrapHolder
}
As it stands, this code produces the compiler error in the last holdResource
method where I'm trying to build a HolderOfWrappers
:
Cannot convert the expression's type 'MyResource<A>' to type '(anyWrappedItem: MyResource<Any>)'
Which is understandable as the code indicates HolderOfWrappers can only hold a MyResource built for Any type, not any possible type. What I'm really after with the HolderOfWrappers
is something like this:
public struct HolderOfWrappers
{
let anyWrappedItem: MyResource<>
}
or even MyResource<*>
- I am trying to say with this code that I'd like a variable that can hold any type of MyResource. If I try to use either syntax though, I get a compiler error that it expects a type.
I could just have anyWrappedItem
by of type Any
, but then you lose the type information for future use. I also do not want HolderOfWrappers to be generic (because then I'd just have the same problem later).
It's almost like I am trying to treat the generic type as a protocol for the anyWrappedItem
storage variable, which will not work for other reasons...
I think you can do what you want by putting a generic parameter in your HolderOfWrappers
init
method. Basically, the init
method just generates a new MyResource
using the resource
that you provide, like this:
public struct HolderOfWrappers {
let anyWrappedItem: MyResource<Any>
public init<A>(resource: MyResource<A>) {
self.anyWrappedItem = MyResource(wrappedItem: resource.wrappedItem, convert: resource.convert)
}
}
I think that will do what you want. I don't know if it will be slower since you are initializing an entirely new MyResource
instead of just copying one.
At any rate, it makes it so that HolderOfWrappers
itself is not generic and will fill anyWrappedItem
with a MyResource<Any>
that holds the same values as the resource
that you pass in.
这篇关于Swift:如何在变量中保存泛型类型的任何可能实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!