4

Im trying to create a generic lambda so I dont need to redefine it for individual types. To do that I need to access the type parameters that were passed to the function. Unfortunately I didnt find any documentation on how to do that.

This is an example of what I want to do but the in front of the lambda wont compile:

import java.util.ArrayList;

public class Test {

    interface SolveFunction {
        <In, Out> Out apply(In in);
    }

    public static void main(String...args) {
        SolveFunction f = <In, Out> (In a)  -> {
            ArrayList<Out> list = new ArrayList<>();
            return list;
        };
        System.out.println(f.<String, Integer>apply("Hi").size());
    }
}
3
  • What are you trying to do exactly? There are many things in this code that won't compile... Commented Nov 19, 2013 at 13:55
  • Im trying to implement the Divide and Conquer Pattern which accepts 4 functions: Divide, Solve, Trivial and Combine, heres the larger piece of code why I want generics inside the lambda dpaste.de/R6ew#L76 (code not tested yet since it breaks when at the generics when i try to compile it) Commented Nov 19, 2013 at 13:58
  • FYI this is not production code and I know that Java is not inteded to be used as a functional language. Commented Nov 19, 2013 at 14:05

2 Answers 2

7

First of all, add the type arguments to the interface itself:

interface SolveFunction<In, Out> {
    Out apply(In in);
}

You could implement a method:

public static <In, Out> List<Out> makeList(In in) {
    return new ArrayList<Out>();
}

Which you can then use as the implementation of the lambda:

SolveFunction<String, Integer> f = Test::makeList;

System.out.println(f.apply("Hi").size());
Sign up to request clarification or add additional context in comments.

1 Comment

Note also that you wouldn't even need your SolveFunction interface, there's already an interface in the standard library that looks the same: java.util.function.Function<T, R>.
0

I'm unsure about what you're trying to use your ArrayList for but you need to add the type declaration to the interface as well. The following example will compile:

public class Test {

    interface SolveFunction<In, Out> {
        Out apply(In in);
    }

    public static void main(String... args) {
        SolveFunction<String, Integer> f = new SolveFunction<String, Integer>() {

            @Override
            public Integer apply(String in) {
                return in.length();
            }
        };

        System.out.println(f.apply("Hi")); // Prints 2
        System.out.println(f.apply("HELLO")); // Prints 5
    }
}

1 Comment

But it doesn't use Java 8 lambda syntax and the method uses concrete types (String and Integer) and not type parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.