Hello everyone.
I have this HUGE class containing way too many functions. It could be shortened to 1/5 if I could only use multi typing in the functions.
For example, the following functions:
Code:
public static Object Choose(boolean Expression, Object TrueValue, Object FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static int Choose(boolean Expression, int TrueValue, int FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static int[] Choose(boolean Expression, int[] TrueValue, int[] FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static Texture Choose(boolean Expression, Texture TrueValue, Texture FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
public static String Choose(boolean Expression, String TrueValue, String FalseValue) {
if (Expression) return TrueValue;
return FalseValue;
}
I know there is a simple way of choosing between objects only, but it requires type conversion after the function. So, how could I shorten it to something like:
Code:
public static class Choose(Type T) {
T Choose(boolean Expression, T TrueValue, T FalseValue) {
if (Expression) return TrueValue; else return FalseValue;
}
}
Thanks in advance.
EDIT
I was pretty close to the resolution, found out that it was called "Generics" in the Java language:
Code:
public static <T> T choose(boolean Expression, T TrueValue, T FalseValue) {
if (Expression) return TrueValue; else return FalseValue;
}