Hello OOPer,
What I want is some way to be able to group some structs and then, a function to be able to receive as argument one of the structs (preferably it's type and not it's instance) in that group, do some calculations with it and then return it, but in a way that the code that called this function, would be able to get the type it wants without returning Any and then have to cast each of the results to the expected type.
Example
(I know that my example is a bit weird and complicated, but i would like to do something like this if possible. It's kind of a dynamic variable type):
struct typeOne: TypeGroup {
var id: Int = 0
var type: String = "test"
var general: String = "test"
static var descString: String = "This is a String"
}
struct typeTwo: TypeGroup {
var id: Int = 0
var number: Int = 0
var general: String = "test"
static var descNum: Int = 0
}
struct typeThree: TypeGroup {
var id: Int = 0
var kind: String = "test"
var general: String = "test"
static var descType: String = "This is a type"
}
func typeFunc(typee: TypeGroup) -> TypeGroup {
/* Something like this: */
result: typeOf(typee) = typee.init()
result.general = "Type: \( typee.descType)"
/* or this: */
var result = typee.init()
switch typeof(typee) {
case typeOne:
result.general = "String: \(typee.descString)"
break
case typeTwo:
result.general = "Number: \(5 + typee.descNum)"
break
case typeThree:
result.general = "Type: \( typee.descType)"
break
}
return result
}
/* Then be able to do this: */
var typeResult: typeOne = typeFunc(typee: typeOne.Type)
/* or this: */
var typeResult: typeTwo = typeFunc(typee: typeTwo.Type)