Heiholf
Heiholf
CC#
Created by Heiholf on 7/22/2023 in #help
❔ Convert void to bool
I know that this question is totally weird and I would never try to use this in clean/maintainable code. However that is not my goal. I'm writing code for Sebastian Lague's Chess Bot Tournament and try to reduce the number of tokens in code (and cannot use unsafe environments). I have three functions like this:
void A() {}
bool B() => false || true; //return value is obviously not the same every time
void C() {}
void A() {}
bool B() => false || true; //return value is obviously not the same every time
void C() {}
And I want to execute them in order and keep the return value of B(). One way to accomplish is like that:
string Execute()
{
A();
bool res = B();
C();
return res ? "Result 1" : "Result 2";
}
string Execute()
{
A();
bool res = B();
C();
return res ? "Result 1" : "Result 2";
}
But it seems to be not the optimal way to minimize tokens. I would except to be a workaround like this:
string Execute2() => A().toFalse() || (B() && C().toTrue()) ? "Yes" : "No";
string Execute2() => A().toFalse() || (B() && C().toTrue()) ? "Yes" : "No";
However, I was unable to find a way to bodge the toTrue/toFalse statements and Google didn't help because it just shows questions of people not understanding what void is. One could obviously wrap A in a function, which invokes it and returns a bool but it generates to many "boilerplat" tokens. Is there any way to achieve this or is C# to securely designed to do this?
22 replies