Custom tags on functions, what the?

Started by TechNick6425, August 09, 2013, 11:03:32

Previous topic - Next topic

TechNick6425

I want to create a game development kit for Java, similar to XNA. I need to have a function that loads stuff from the hard drive. I want it to look a little bit like this.

Model3D model = Content.Load<Model3D>("On/The/Hard/Drive");


I want the only thing that gets used in the load functions is Objects that extend a content class. Is it possible to do this?

Fool Running

Yes, but you should really just try it instead of asking. :P
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

TechNick6425

I did try. I got frusterated. So, I asked here.

quew8

You can either ask a more specific question (at the moment you are asking "can you write this for me") or you can read this.
http://docs.oracle.com/javase/tutorial/index.html. Presumably you are familiar with c# or some other .NET language since you're familiar with xna, so you should breeze through the tutorials.

TechNick6425

I'm sorry, I didn't realize how I was wording it! I was trying to make it, but I wasn't sure how I could get the brackets to work.

quew8

The topic I think you need to read specifically is generics, but this is a vast topic and no one could sum it up in a single post.

TechNick6425

I've read that section, but what I want to do is add generics to FUNCTIONS, not objects.

TechNick6425

I'm also wondering if I could return the loaded content, somethine like public static E load;

quew8

http://docs.oracle.com/javase/tutorial/extra/generics/methods.html maybe you haven't read it as completely as you think. Yes you can return a generic type as well. It can be problematic to create an instance of a generic type at runtime due to type errasure but it is by no means impossible.

Here's an example of some very basic generic functions that concatenate a 2 dimensional array, or several 1 dimensional arrays into 1 single 1 dimensional array.

/**
     * 
     * @param <E>
     * @param arrays
     * @param totalLength
     * @return 
     */
    public static <E> E[] concatArrays(E[][] arrays, int totalLength) {
        E[] result = Arrays.copyOf(arrays[0], totalLength);
        for(int i = 1, pos = arrays[0].length; i < arrays.length; i++) {
            System.arraycopy(arrays[i], 0, result, pos, arrays[i].length);
            pos += arrays[i].length;
        }
        return result;
    }
    
    /**
     * Assumes all arrays are the same size.
     * @param <E>
     * @param arrays
     * @return 
     */
    public static <E> E[] concatArrays(E[][] arrays) {
        return concatArrays(arrays, arrays.length * arrays[0].length);
    }
    
    /**
     * 
     * @param <E>
     * @param arrays
     * @param lengths
     * @return 
     */
    public static <E> E[] concatArrays(E[][] arrays, int[] lengths) {
        return concatArrays(arrays, sumArray(lengths));
    }
    
    /**
     * 
     * @param <E>
     * @param arrays
     * @return 
     */
    public static <E> E[] concatVariableLengthArrays(E[][] arrays) {
        return concatArrays(arrays, arrayOfLengths(arrays));
    }


Simple but effective.