hanjos / genericcons

"Varargs" for Java generics

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CI Javadocs Maven package

Who said Java generics can't accept an open-ended number of type variables? Just write something like

Fn<?, ?> f = new Fn<Integer, C<String, C<String, String>>>() {
  public Integer execute(Object... objects) {
    List<? extends Type> types = Types.fromSuperclass(this.getClass(), 1);
    
    if(! Types.check(types, Arrays.asList(objects))) { // the given objects don't match!
      return -1;
    }
  	    
    String a = (String) objects[0];
    String b = (String) objects[1];
    String c = (String) objects[2];

    return Math.max(a.length(), b.length(), c.length());
  }
};

Hey, I never said the solution was pretty. Or useful :)

How?

Some type token chicanery and good ol' cons for inspiration.

Why?

It occurred to me one day, and I haven't contributed to Java's grand tradition of gross overengineering for quite some time, so there you go :P

Usage?

Basically, Maven and GitHub Packages:

  1. Add https://maven.pkg.github.com/hanjos/genericcons as a repo in your pom.xml;
  2. Set up a personal access token in your settings.xml with package reading rights;
  3. Add the dependency in your pom.xml. The versions available can be seen here.
  4. You're good to go!

Of course, one can always download the code and mvn install...

About

"Varargs" for Java generics

License:MIT License


Languages

Language:Java 100.0%