I don’t see how broadcasting would be better. Your example will end up calling interface_get
with single elements, so you will be calculating prework
over and over again. One way to solve that is using caching / lazy initialization like in Tamas’ link above (see the Lazy struct I proposed). Then you could push the calculation of prework all the way to the lookup method (where the prework is accessed). Whether that’s better or not is hard to tell – in my experience lazy variables can make the code slightly more complex and increase the risk of errors, and the lazy indirection will also add a small overhead to the lookup (few nanoseconds if cached; probably acceptable unless the lookup is trivial like in the example above).
1 Like