Results 1 to 1 of 1

Thread: [RESOLVED] "Template" or "Multi Type" function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Resolved [RESOLVED] "Template" or "Multi Type" function

    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;
    	}
    Last edited by bergerkiller; Mar 24th, 2011 at 04:15 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width