I have a function that needs to receive a type, determine the subtype, then create a copy of the subtype. I keep staring at this function and I feel this can be simplified, but I just can't figure out how to get it functioning. Here's the code:
func AddProp(prop prop, x, y int) []prop {
v := reflect.ValueOf(prop)
fmt.Printf("Prop type is %v", v.Type())
switch v.Type() {
case reflect.TypeOf(&Fence{}):
f := Fence{}
f.New(x, y)
props = append(props, &f)
case reflect.TypeOf(&Rock{}):
f := Rock{}
f.New(x, y)
props = append(props, &f)
}
return props
}
It's actually quite a bit longer than this with many more cases. Thank you for taking a look!
Aucun commentaire:
Enregistrer un commentaire