5 ms·
Java designers have really shot every language user in the foot by eschewing the notion of a function from the language. The only way to have a function is to c
by nine_k 5y ago
Java designers have really shot every language user in the foot by eschewing the notion of a function from the language. The only way to have a function is to create a class with a single static method.
Hence you can't have function composition, instead you get dependency injection, with an extra layer of XML files in most frameworks to allow injecting different stuff without recompiling the classes without access to the source, or just obviating the need to recompile.
DI is a nice concept, and is used to solve a real problem of declaratively configuring closed-source code. I bet that declarative DI implemented for CL or Scheme would look more compact and neat than the typical Java approach.
- IshKebab 5y agoA static class method is exactly the same as a free-standing function, it's just in the class namespace. I don't see why you think that means you can't have function composition.
- gowld 5y agoNot true. It's not first class. You can't pass a static class method to a method and then call it. You have to instantiate a Function instance or equivalent.
- IshKebab 5y agoOk but that's a separate weird limitation that only applies to Java. You can pass static methods as parameters in at least C++, Python and Javascript.
- UncleMeat 5y agoThat is orthogonal. Java could easily have free standing functions that are still not first class.
- native_samples 5y agoThat's not actually true. You can use method handles to do that.
- nine_k 5y agoOnly if you mark that class with `@FunctionalInterface`. Unfortunately, methods are not first-class objects in Java (while classes, with .class, are).
- pjmlp 5y agoWhat people like to blame Java for, was already common practice in enterprise C, C++, Smalltalk, VB and Delphi before the language came to be. Why do you think the Gang of Four book uses Smalltalk and C++ without any traces from Java?
- deleted 5y ago[deleted]
- bostonpete 5y agoProbably in large part because their book came out before Java was even around. :-D
- joeberon 5y agoExactly the point they were making. All these problems were already around at the time. In fact people didn't even see them as problems. Read gang of four, and they don't really mention working around the limitations of the language. It is as if they are not really aware how many of their problems are arising due to the language's design, and think their patterns are just a generically good to do it. People don't use a majority of them now because there is just no need for them anymore in modern languages.
- hinoki 5y agoSo the Sapir-Whorf hypothesis is true for programming languages?
- nine_k 5y agoCuriously, Smalltalk also lacks the idea of a function. Everything is an object; instead of a function, you have a code block which is an object. But, unlike Java, Smalltalk is (utterly) dynamic, and you don't have to declare a class for your code block. In Java, you had to dance that dance, until lambdas were introduced to let the compiler do that for you.
- 5y ago
- twic 5y ago> Hence you can't have function composition No, the thing that stopped Java having function composition was the lack of (ergonomic) closures. But it got closures in 2014, and now you can do function composition. Despite the fact that it still doesn't have top-level functions. I still don't really understand where all the XML came from, or the wave of equally unnecessary annotation-based dependency injection that came afterwards. There has been a dream of "write reusable components, then compose them to form applications" that has been very strong in Java right from the outset, and i tentatively think that people just didn't get that you could do the composition in code.
- nine_k 5y agoIndeed! No functions, no closures, and no support for either on the JVM bytecode level. Now you can have lambdas, and they go to a generated class as methods, so that the JVM could execute them. Where XML came from: how do you configure WebSphere in 2000? XML was all the rage since mid-1990s. It had a ton of advantages, back when you did not have JSON, YAML, or TOML.
- ngc248 5y agoJava is an amazing language ... but somehow all the ills of J2EE are blamed on Java. Back in the 2010s itself there were a lot of frameworks which were supporting both declarative and code as config. But, it is fashionable to shit on Java.
- deleted 5y ago[deleted]
- matheusmoreira 5y ago> people just didn't get that you could do the composition in code Yeah, Java does that to people. It's such a limited language that it becomes impossible to conceive of better ways to solve problems. Apparently it's come a long way since Java 6, it's even broken backwards compatibility, never expected that to happen. It's adopted many functional concepts in the usual insanely verbose Java style. Still a very limited language though, especially when compared to real functional and object-oriented languages. It's limited even when compared to C.
- nuerow 5y ago> Java designers have really shot every language user in the foot by eschewing the notion of a function from the language. The only way to have a function is to create a class with a single static method. Java does support lambda expressions. Also, the fact that in Java everything is a class is something that cuts both ways. As you're already going to have classes, you can have your pure functions as static member functions from any class that suits your fancy. That pretty much works as a function within a namespace.
- mishafb 5y agoThe lambdas aren't actually lambdas, they are anonymous classes with a single metthod according to the type-inferred interface, and they can't use variables that aren't effectively final.
- gpderetta 5y agoThat's just a way to make them work in the existing ecosystem (in C++ lambdas for example are just function objects with overloaded operator()). The final is to avoid some mistakes, but they are still effectively closures.
- pyentropy 5y agoHaven't touched Java 8+ in a bit, but I remember Function2, Function3, Function4<P,Q,R,S> etc when referring to a closure. Streams used a ton of specializations like ToDoubleBiFunction<P, Q> meaning (P p, Q q) -> double. I wonder if such contracts are are solved now like in other languages with typed function references/pointers - C++/Go/etc (syntactic sugar is enough, doesn't have to be variadic generics). I remember 'var' was introduced so I suppose that worked if the closure type could be inferred.
- tikkabhuna 5y agoI've always viewed the @FunctionalInterface interfaces in Java 8 as very similar to Rust's Option and Result. Sure they're just like any other interface, but you can do special things with them because of their properties (single method). I would take that over Go's special syntax for map any day as I now have the power to create my own functional interfaces and plug them into Java's standard library.
- matheusmoreira 5y agoYeah. Java's design mistakes directly lead to these design patterns being used constantly to work around the limitations of the language. It's such a drag. Even if you have static methods, you still need to use this stuff because they are methods, not functions. Classes aren't objects so they can't implement interfaces or be passed around as parameters. Gotta make things like singletons and factories because of this just so you can essentially pass a function pointer around. There's names for stuff that in better languages just happens naturally without people even thinking about it.