For the sake of increasing code readability, I want to create a custom iterator called “do unless”, that works similar to “do while”, but it repeats the body unless the condition is met. (In essence, “do body unless (condition)” is exactly equal to “do body while (!condition)”, but as I said, code readability increases in many cases if I have both options).
I have a public static class called Utilities where I’m defining custom functions to help me code, so I think that is the right place to define it. I created the following definition (inside the static class):
public static class Utilities {
// The opposite of "do while"
public static void doUnless(System.Action body, bool condition)
{
do { body(); } while (!condition);
}
}
I believe in Javascript, something similar to what I wrote could work, but in C# it’s not that simple. I tried testing this in a number of ways, but none seem to work. For example:
public class SomeClass: MonoBehaviour {
public int test = 0;
public void Testing()
{
test++;
print(test);
}
void Start()
{
Utilities.doUnless( Testing(), test > 10);
}
}