Hello Guest

Custom tags on functions, what the?

  • 9 Replies
  • 6303 Views
Custom tags on functions, what the?
« on: August 09, 2013, 11:03:32 »
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.

Code: [Select]
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?

Re: Custom tags on functions, what the?
« Reply #1 on: August 09, 2013, 12:22:24 »
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

Re: Custom tags on functions, what the?
« Reply #2 on: August 12, 2013, 11:22:10 »
I did try. I got frusterated. So, I asked here.

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Custom tags on functions, what the?
« Reply #3 on: August 12, 2013, 15:12:35 »
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.

Re: Custom tags on functions, what the?
« Reply #4 on: August 12, 2013, 15:45:53 »
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.

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Custom tags on functions, what the?
« Reply #5 on: August 12, 2013, 16:50:51 »
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.

Re: Custom tags on functions, what the?
« Reply #6 on: August 13, 2013, 00:37:57 »
I've read that section, but what I want to do is add generics to FUNCTIONS, not objects.

Re: Custom tags on functions, what the?
« Reply #7 on: August 13, 2013, 00:59:39 »
I'm also wondering if I could return the loaded content, somethine like public static E load;

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Custom tags on functions, what the?
« Reply #8 on: August 13, 2013, 09:32:35 »
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.

Code: [Select]
/**
     *
     * @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.

Re: Custom tags on functions, what the?
« Reply #9 on: August 13, 2013, 11:05:22 »
Thanks!