Hi I'm trying to run a proc that uses generics. There will be a general update[T] proc for normal use, and a specific proc for any custom code I would want to add. eg update[Enemy] for any enemy specific code.
Here is a code example of what I mean:
type EntGroup[T] = ref object
items: seq[T]
proc update[T](self: EntGroup[T]) =
echo "general update loop"
proc update(self: EntGroup[Enemy]) =
## << What to put here to call the `update[T]()` code? ##
echo "enemy code"
var eg = EntGroup[Enemy]()
eg.update()
# should output the following:
# "general update loop"
# "enemy code"
Is such a thing possible?type
Enemy = ref object
EntGroup[T] = ref object of RootObj
items: seq[T]
EnemyEntGroup = ref object of EntGroup[Enemy]
method update[T](self: EntGroup[T]) =
echo "general update loop"
method update(self: EnemyEntGroup) =
procCall update(EntGroup[Enemy](self))
## << What to put here to call the `update[T]()` code? ##
echo "enemy code"
var eg = EnemyEntGroup()
eg.update()
# should output the following:
# "general update loop"
# "enemy code"
I mean, in general you can just call the generic explicitly with update[Enemy](self) like so:
type
Enemy = ref object
EntGroup[T] = ref object
items: seq[T]
proc update[T](self: EntGroup[T]) =
echo "general update loop"
proc update(self: EntGroup[Enemy]) =
## << What to put here to call the `update[T]()` code? ##
update[Enemy](self)
echo "enemy code"
var eg = EntGroup[Enemy]()
eg.update()
Outputs as you expect. Generally though what you're currently implementing looks to me more like you're thinking in patterns of dynamic dispatch with method (Namely that there's a "base-entity" that you can perform a "base-task" on that should always be run).
If that's how you think about your code then imo it's best you also implement it in that fashion using methods etc. as blackmius suggested.
Oh that was easy! Thanks! Didn't occur to me that the square brackets explicitly calls the generic function (though it makes perfect sense now in hindsight).
Thanks everyone for the help! Pretty cool that there are lots of solutions to this.