You can do this:
let things: [Any] = [intThing, stringThing]
Thing is not a valid type on it's own. Thing<String> is a type and Thing<Int> is an other type and you can't mix different type within an array.
Have you notice that even let things: [Thing] doesn't compile?
I think that what you are probably trying to do would be better server with an enum with associated values.
struct MyStruct<T> {
let property: T
init(property: T) {
self.property = property
}
}
enum Thing {
case int(Int)
case string(String)
case intStruct(MyStruct<Int>)
}
// creation
let myInt = Thing.int(2)
let myString = Thing.string("string")
let myIntStruct = Thing.intStruct(MyStruct<Int>(property: 3))
// down side is that you need a 'case' clause to retrieve the data
switch myIntStruct {
case let .int(value):
print("have a int: \(value)")
case let .string(value):
print("have a string: \(value)")
case let.intStruct(value):
print("have a intStruct\(value)")
}
// An array of Thing
let things: [Thing] = [myInt, myString, myIntStruct]
nice article about advance enum trick here
In this file about wrapping Plist data into a single structure, there is the use of an enum for the EntityType.
You can associate anything to an enum value, but it needs to be a fully qualified type. So you can't use MyStruct because it is not fully qualified, you need to use MyStruct<Int> to fully qualify. This means that you would need to create a case for every generic type you want to use with MyStruct.
That is as far as you can go with your generic approach.
I'm not sure what you are trying to do. But if you want to achieve some kind of polymorphism that relies on methods that operate on T but don't use T as input nor output you should make your Thing implement a protocol and make an array of that protocol and call your polymorphic method on that protocol.