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?

SoftMemes
  • 5,602
  • 4
  • 32
  • 61
  • 2
    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! – Gabe Sep 04 '11 at 02:15
  • See also http://java.sun.com/docs/white/sidebar.html – Gabe Sep 04 '11 at 02:19
  • 2
    "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... – Thomas Levesque Sep 04 '11 at 02:51
  • 1
    @Gabe be fair, they changed many other things so that MS java would not be multi platform. – mP. Sep 04 '11 at 03:06
  • @Gabe - that's a very interesting link, thanks for that! – SoftMemes Sep 04 '11 at 10:25
  • See also http://stackoverflow.com/questions/1184418/javas-equivalents-of-func-and-action – anre Aug 23 '16 at 14:55

2 Answers2

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

Bear Vast
  • 150
  • 1
  • 8
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 ;).

Arnout Engelen
  • 6,709
  • 1
  • 25
  • 36
  • 1
    `Action<>` is "overloaded" to handle no return and zero to N parameters (currently 16), so `Callable` is useless there. In fact, after reading the `Callable` description, I guess Java's `Callable ` can only be mapped on C#'s `Func`, (which takes zero parameters, and returns T) and nothing else. – paercebal Sep 04 '11 at 00:34
  • Thanks, so interfaces are probably often, even though technically very different, in practice a reasonable substitute for Action. Updated the answer to reflect that. – Arnout Engelen Sep 04 '11 at 00:42
  • 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... – SoftMemes Sep 04 '11 at 00:53