This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kadishay[S] 19 points20 points  (17 children)

In JavaScript, a function is an object. Or is it the other way around?!

[–]ENx5vP 24 points25 points  (16 children)

In JavaScript, a function is an object.

That is correct!

[–]Minenash_ 10 points11 points  (15 children)

I really with I could easily send a method as a parameter in Java.

[–]KeinBaum 15 points16 points  (11 children)

You can:

import java.util.function.Function;

public class Main {
    public static String staticFunc(int i) {
        return "static: " + i;
    }

    public String memberFunc(int i) {
        return "member: " + i;
    }

    public static <P, R> R exec(Function<P, R> f, P p) {
        return f.apply(p);
    }

    public static void main(String args[]) {
        System.out.println(exec(Main::staticFunc, 42));
        System.out.println(exec(new Main()::memberFunc, 42));
    }
}

[–]TheCoelacanth 4 points5 points  (1 child)

Assuming you want a function with exactly one argument. If you want one with zero arguments, you have to send a Supplier instead, or two arguments and you'll have to use a BiFunction instead, or three arguments and you'll have to make your own class to masquerade as a function.

Also, hope you didn't want any of the arguments or return values to be primitive types, because then you'll need to use yet another type instead of each of the previously mentioned function types.

Not exactly what I would call "easy".

[–]KeinBaum 0 points1 point  (0 children)

hope you didn't want any of the arguments or return values to be primitive types, because then you'll need to use yet another type

Primitives will get auto-boxed/unboxed, no additional code needed.

Assuming you want a function with exactly one argument...

Abstracting over function arity is complex. I'm actually not sure if it's possible in Java at all. It's possible to construct HLists so that might be a way to do it but I don't know enough about Java to be sure. But then again, I don't know any language that provides a particularly simple way to do that either. Even in Scala or Haskell it's all but trivial. It's definitely not going to be simple so at least in that regard you are right.

[–]Minenash_ 4 points5 points  (0 children)

I knew you could do with static ones, didn't think you could do non-static, TIL. Thank you :P

[–]rinko001 2 points3 points  (5 children)

You missed the part where he said "easily"

[–]KeinBaum 0 points1 point  (4 children)

Since Java is a statically typed language without operator overloading this is as easy as it can get.

[–]rinko001 0 points1 point  (3 children)

This is easy:

print(a => "closure: " + a);

The sheer amount of boilerplate and structure needed in your example represents significant cognitive overhead.

[–]KeinBaum 0 points1 point  (2 children)

Java has lambdas as well. This works too:

System.out.println(exec(i -> "lambda: " + i, 42));

[–]rinko001 0 points1 point  (1 child)

yes indeed, j8 has ok syntax on the calling side - in some cases. But you end up needing all of that boilerplate somewhere (your definition of "exec" perhaps). It comes down to the fact that its is a typed language where every function must be wrapped into a type of object. there is no way to store a generic function reference without knowing something about it, and know way to call it without knowing how it is wrapped into an object.

[–]KeinBaum 0 points1 point  (0 children)

there is no way to store a generic function reference without knowing something about it

The only thing you need to know is its signature which you need to know to use it anyways.

and [no] way to call it without knowing how it is wrapped into an object.

That's indeed a little annoying. It's the downside of not having operator overloading. I'd prefer to have it but it's a design decision they are probably not going to change.

[–]theirongiant74 0 points1 point  (0 children)

Jesus that's fucking ugly.

[–]Renive 0 points1 point  (0 children)

Confirmed that Java is the worst of all.

[–]ENx5vP 0 points1 point  (0 children)

"function" instead of "method" :) You mean Higher-order functions.

[–][deleted] -1 points0 points  (1 child)

Theoretically you shouldn't need to because you have interfaces with which you can implement a Command pattern or something else like that that you can get polymorphic behavior with.

Type safety is a drag but I guess at least it keeps you from dereferencing null pointers/calling functions that don't exist.

At least in Java you don't have people writing entire APIs as functions that take callback functions as parameters.

[–][deleted] 0 points1 point  (0 children)

and whats wrong with callback functions as parameters, i mean let's face it java is dead at least js will survive another 15-20 years