Hi,
I am stuck at something that I find very difficult to do in Zig (for good and obvious reasons), which is elementary in other languages.
Java/C# type Pseudo code
interface QuitStrategy {
bool execute();
}
class IterationQuitStrategy {
@override bool execute() {
return (cycles > 10);
}
}
class MyExecutor {
@Getter @Setter private QuitStrategy strategy;
public void run() {
if (strategy.execute()) {
setTerminate(true);
}
}
}
There are two derivations to this:
- Having struct and method references so that, at compile time, the method, signature and instance of the struct is known. (second prize, but still no totally sure how to do… the source Thread.spawnThread talks to it somewhat).
- The more difficult one (I believe), to keep a reference to the strategy instance and the method pointers… as to invoke it by its implied signature with the instance as a parameter. This in my mind will make the it not comptime known, which voids optimization BUT it gives one the option to change the behaviour at runtime (e.g. by configuration).
I have looked for answers, but the threads I found does not talk to method of a struct as a member of a struct, callable by methods of the container struct.