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...