3 ms·
You can just generate the 'vtable' as code :) switch (animal.type) case Cat: return cat_speak() case Dog: return dog_speak() The generated code has
by fooker 5mo ago
You can just generate the 'vtable' as code :)
switch (animal.type)
case Cat: return cat_speak()
case Dog: return dog_speak()
The generated code has the functions resolved in compile time, there's no function pointer lookup in a table happening. I don't know if this is how this project does it, but this is the commonly used technique when you want to do this.
- jeswin 5mo agoHmm yeah good point. I didn't think of it. It might even be cheaper to do this when the list of possible types are closed and few. I am still inclined to believe AI just made up the documentation though, because this has its own tradeoffs.
- fooker 5mo ago> might even be cheaper to do this Oh yeah, very often. Especially if it's a loop and resolves the same way very often. Modern CPUs just blaze through code like this, after three decades optimizing for object oriented and dynamic languages.