What is the closest thing Java has to the .NET Func<> and Action<> delegates?
Asked Answered
B

2

8

Obviously, Java does not have delegates or functions as first class values and uses interfaces instead, but what is the closest interface to the Func<> or Action<> .NET delegates? There is Runnable and Callable<>, but only in flavors taking no parameters.

As Java cannot have overloaded types with the same name and different number of generic type arguments, I understand that there cannot be a single shared interface name, but there could be a Runnable1<>, Runnable2<> and so on.

Is this style of programming just not used in Java or am I missing any existing interfaces?

Bowlds answered 4/9, 2011 at 0:8 Comment(6)
Back before MS created C#, they had an implementation of Java (they called it J++). When they tried to add delegates to it (among other things like enums), Sun sued them and said they couldn't ship Java anymore. And that's why they created C#. If you search for articles about Java delegates, you'll find many people decrying the addition of delegates to Java and suggesting that anonymous inner classes implementing interfaces is much better!Comrade
See also java.sun.com/docs/white/sidebar.htmlComrade
"Java cannot have overloaded types with the same name and different number of generic type arguments": yet another reason why type erasure was the wrong approach to generics...Averell
@Comrade be fair, they changed many other things so that MS java would not be multi platform.Flowers
@Comrade - that's a very interesting link, thanks for that!Bowlds
See also #1184918Carpeting
S
0

I'm not very familiar with .Net, but from what I've seen so far it seems you would usually create an interface for each type of function/action you want to store. This is not really the same but sounds like what you're looking for.

(the interface is not anonymous, it has a name, a meaning and documentation, which might make code easier to read. On the other hand it is an extra hassle to have to write it, and you cannot just throw in any function that matches the signature).

Do you have a specific use case for which you're seeking a recommendation? It's hard to make generalized statements ;).

Supplemental answered 4/9, 2011 at 0:17 Comment(3)
Action<> is "overloaded" to handle no return and zero to N parameters (currently 16), so Callable<T> is useless there. In fact, after reading the Callable<T> description, I guess Java's Callable<T> can only be mapped on C#'s Func<T>, (which takes zero parameters, and returns T) and nothing else.Lauder
Thanks, so interfaces are probably often, even though technically very different, in practice a reasonable substitute for Action<T>. Updated the answer to reflect that.Supplemental
Func<> and Action<> are very useful when using higher order functions. If I want to map all items in a list to another one by one, I give a Func<,> to Select() to do so. It could even in .NET be a named delegate type, which would be the closest match to a named specialized interface - but the beauty of having them anonymous lies in the fact that functions can be used for things they were not specifically created for...Bowlds
R
0

I've created a jar for you, and for myself :)

maven info:

<dependency>
    <groupId>com.incarcloud</groupId>
    <artifactId>ac-func-tion</artifactId>
    <version>1.1.0</version>
</dependency>

then you can write code like c#

import com.incarcloud.lang.*;

Action<Integer> actFoo = (x)->{ System.out.println(x); }
actFoo.run(1024);

Func2<Integer, Integer, Integer> actAdd = (x,y)->{ return x+y; }
int sum = actAdd.call(5,6);

More information can be found here https://github.com/InCar/ac-func-tion

Roydd answered 23/4, 2018 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.