6

I am always hearing people talk about how Java does not support first class functions, that it is an advantage you get from functional languages, the only way to simulate it in Java is through the use of anonymous inner classes etc..

However, from what I can see Java does support first functions through the use of its reflection API. For example I can create a method object from a class and then call it on a number of objects of that class. I realize it is not as powerful as first class functions in other languages. For example in Python you can do the following:

class Test:
    def __init__(self, num):
        self.number = num
    def add(self, num):
        self.number += num
test = Test(1)
method = test.add
method(2)

You cannot do this in Java because you need to have a reference to the object you want to invoke the method on. However you can still treat the method as an object which is what defines first class functions. I guess the method object is not really the actual function but rather a meta-data object, although using reflection it can be treated as such. Maybe I just need clarification on what defines a first class function.

6

A first class function is one where the function is available on its own. C, C++, and Ruby allow this approach. Java requires the function to be tied to a class and only provides a metadata representation of it, even if that class is merely a static collection of functions. C# supports first class functions with lambdas (which are based off of lambda calculus) and delegates.

Ruby is one of the languages that truly supports first class functions. The difference is that not only can you define functions on their own, but you can pass them as arguments and invoke methods on them. Check out Ruby's Proc object which is used to represent an arbitrary block of code.

The end result of having first class functions is the fact that it lends to some very powerful and flexible coding constructs. This is distinctly different than hacking around first class functions using the reflection API.

Java doesn't have full support of first class functions. The reflection API can give you some semblance of first class functions if the Method object is referencing a static method. In essence you can invoke a static method like this:

Method reference = mathClass.getMethod("sqrt");

// NOTE: the first parameter is for the object instance,
// but for static methods it is ignored
double answer = (double)reference.invoke(null, 4);

As soon as you are working with an instance method, you lose that first class function ability. You might be able to hack together some reflection based delegate support similar to C#, but the resulting code will be much slower. The "delegate" would take care of keeping the object reference for future invocations.

  • Yea, I see what you mean. Hmm, Is there much benefit in using that proc object over a plain old closure? – toc777 Apr 1 '11 at 16:07
  • In Ruby, the Proc object is implicitly created for you. It's the type of any block of code--from a full function to a lambda or a closure. Different languages have different ways of representing the same concept. The Wiki I linked to provides a feature list for first class functions (note: Java doesn't meet any of those features). – Berin Loritsch Apr 1 '11 at 16:28
6

First class functions are not functions outside classes, first class functions are functions that can be referenced and passed to other functions, just as integers or objects can be. Calling a function via reflection, is close -- it allows you to eventually do the same thing, but it has an extra level of abstraction which makes it harder to use. You have a wrapper around the function, and you can pass that around, but you don't have a direct reference to the function.

Java 8 made functions first class with the introduction of method references. You can have a method that explicitly takes a function as an argument, and then call that function from within the method, but you don't have that *wrapper -- which makes using it more straightforward.

*Yes, I know, there actually is a hidden wrapper in some cases, but that is an implementation detail, which they are free to change.

-1

the only way to simulate it in Java is through the use of anonymous inner classes etc..

This is simply wrong. Nothing prevents us from declaring a top level class that has only method(s), and treat instances of that class as functions. If the class has indeed no state, it tuns out that we need only ever one instance of it, but that is a technical detail.

Therefore, I'd say that first class functions is a language feature the language designers were aware of, and wanted to support it explicitely.

  • 5
    This isn't correct. Supporting first-class functions means having the ability to treat them as objects, the same way one would treat an integer or a floating-point value. You cannot assign a function to a variable in Java, nor can you pass it to a method; the closest you can do is the reflection method in Berin's answer, or through interface pointers to anonymous class instances. – jprete Apr 9 '11 at 17:48
  • 2
    @jprete - I have just sketched how function-objects could be implemented, and you tell me I cannot? This is ridiculous. Yet, I know what I am talking about because I am just doing that stuff in java - everything is possible: values (i.e. objects) that are functions, i.e. can be applied to other values, lazy evaluation, partial application, to name a few. – Ingo Apr 9 '11 at 19:36
  • 4
    Nobody needs to "sketch how function-objects could be implemented" for a language with first-class functions, because such a language already has it natively. – jprete Apr 9 '11 at 19:48
  • 4
    So what, @jprete? Where did I say java had first class functions? Of course they have them not! I just said that anonymous inner classes are not the only way to implement proper functions in java. I recommend "read before downvote" next time. – Ingo Apr 9 '11 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.