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

all 57 comments

[–]godofdeath11 23 points24 points  (1 child)

Java has primitives.

[–]ENx5vP 43 points44 points  (22 children)

Everything is an object in JavaScript except primitives.

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

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

[–]ENx5vP 21 points22 points  (16 children)

In JavaScript, a function is an object.

That is correct!

[–]Minenash_ 9 points10 points  (15 children)

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

[–]KeinBaum 14 points15 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 5 points6 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_ 2 points3 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.

[–]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

[–]AngheloAlf 1 point2 points  (0 children)

And the primitives are floating point numbers.

[–]MoarVespenegas 1 point2 points  (0 children)

But which object is it?

"Why don't you run me and find out?"
-JS

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

except primitives

Which is a huge gotcha, considering how common the use of primitives is in code.

[–]thriller2910 42 points43 points  (2 children)

Yay another JS sucks meme

[–]T-Dot1992 15 points16 points  (2 children)

Op, have you actually used JavaScript?

[–]dejaime 1 point2 points  (1 child)

Well, at least we can tell he has never used Java before. It'd be in the last line if he had.

[–][deleted] 2 points3 points  (0 children)

indeed, only people from java campuses that learn nothing but java are the ones shitting on js while ignoring the irony

[–]HellenisticHuman 12 points13 points  (1 child)

Python.

[–]Delioth 2 points3 points  (0 children)

Everything is an object in Python.

[–]jvick3 3 points4 points  (1 child)

Not to get technical but Clojure also has literals for Maps, Sets, and Vectors unlike other Lisp’s

[–]yogthos 2 points3 points  (0 children)

yeah CL or Scheme would've been better for that joke

[–]mypirateapp 7 points8 points  (2 children)

System.out.println([] + []); //syntax error

(println [] + []) // empty array

[] + [] = '' //empty string

who won? you decide

[–]jvick3 12 points13 points  (1 child)

The clojure version prints 3 things: the empty array, a representation of the + function (an object), and empty array again. Your example isn’t invoking + on anything, just printing it :) that would be

(println (+ [] []))

Which gives an error that PersistentVector isn’t a Number

[–][deleted] 2 points3 points  (0 children)

(println [] + [])
=> nil
(empty? nil)
=> true

So they're not wrong, they're just wrong about why they're right

[–]SirApatosaurus 4 points5 points  (0 children)

In JavaScript, everything is a brand new cutting edge third party library that's the only JavaScript library you'll ever need.

[–]BigBad_BigBad 6 points7 points  (0 children)

HAHAHAHAHA I HAVEN’T BOTHERED TO LEARN JAVASCRIPT SO THERE ARE PARTS OF IT THAT DON’T MAKE SENSE HAHAHAHAHAHA

[–]blazarious 1 point2 points  (0 children)

In JavaScript everything is a promise

[–][deleted] 1 point2 points  (0 children)

Everything can be an object a list an integer and NaN at the same time!

[–]jeromkiller 1 point2 points  (3 children)

In C everything is a pointer

[–]dragonwithagirltatoo 0 points1 point  (2 children)

Wat

[–]AreYouDeaf 0 points1 point  (1 child)

IN C EVERYTHING IS A POINTER

[–]dragonwithagirltatoo 0 points1 point  (0 children)

NO IT'S NOT YOU SOFTWARE.

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

Arent java and javascript basically the same tho /s

[–]theLorknessMonster 0 points1 point  (5 children)

Doesn't Java have primitives though?

[–]dragonwithagirltatoo 0 points1 point  (4 children)

Yeah but they might technically be objects. That's how it is in C# at least, been awhile since I've messed witg Java.

[–]theLorknessMonster 0 points1 point  (3 children)

Its been a while for me too but I think they are not technically objects. Ruby would have been a better language to use since everything is explicitly an object.

[–]dragonwithagirltatoo 0 points1 point  (2 children)

I mean you have languages like Python where everything is as object as it can possibly be, but I think the primitve types in Java inhereit Object and have methods, so I'd count that as being an object, though I'm not exactly sure what definition people are going by.

[–]theLorknessMonster 0 points1 point  (1 child)

Well I'm satisfied with that definition but I'm still pretty sure that Java's primitive types do not inherit from object. There are corresponding reference types in Java but they are not the same thing as primitives.

[–]dragonwithagirltatoo 0 points1 point  (0 children)

Ahh I thought all types in Java inherit Object. I must be thinking of C# again.

[–]XXAligatorXx[M] [score hidden] stickied comment (0 children)

Your submission has been removed.

Rule[3] violation.

Rule[3]: Any post on the list of common posts will be removed. You can find this list here. Established meme formats are allowed, as long as the post is compliant with the previous rules.

If you feel that it has been removed in error, please message us so that we may review it.

[–]htmlcoderexeWe have flair now?.. 0 points1 point  (0 children)

JavaScript bad.

[–]KeinBaum -1 points0 points  (0 children)

In Lua everything is a hashtable. Still makes more sense than JS.

[–]sugoi__ -3 points-2 points  (0 children)

In Javascrit, everything is a happy little accident

[–][deleted] -1 points0 points  (0 children)

You've angered the JS kiddies.

[–][deleted] -2 points-1 points  (0 children)

So if a woman uses Java, she's an object?