2 ms·
So, Java now supports free functions (not associated with some class). Or not really yet?
by Panzerschrek 1y ago
So, Java now supports free functions (not associated with some class). Or not really yet?
- vincnetas 1y agoI think all functions are associated with some class. But these classes sometimes are hidden from you and you don't need to know about them. Does that help you? What for would you use true free functions?
- throwaway47461 1y ago> What for would you use true free functions? The same things people use free functions for in any other programming language.
- rerdavies 1y agoCluttering up global namespaces? ;-P
- deleted 1y ago[deleted]
- Mawr 1y ago> What for would you use true free functions? I'm visibly shaking at this statement. What are you even saying here? A function is the most basic, fundamental unit of code organization. What Java does with its forced classes is a complete perversion of computation, sanity and anything reasonable.
- BinaryRage 1y agoNo, these are just default imports. Module import declarations are in for JDK 25 to make this kind of thing even easier: https://openjdk.org/jeps/511 https://openjdk.org/jeps/511
- TZubiri 1y agoPresumably these exist in a Main.java file and the java class is implied. Similarly the string params are also implied, and the non param main() function is just an alias or a wrapper for main(String). It's just syntactic sugar.
- ninjin 1y agoIf so, are we observing a move towards Universal Suffrage in the Kingdom of the Nouns? [1] [1]: https://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html https://steve-yegge.blogspot.com/2006/03/execution-in-kingdo...
- naruhodo 1y agoNope. It's just sugar. [1] It's somewhat implicit in the JVM spec, but functions are always bound to classes as methods. [2] The only kinds of heap objects you can have a reference to are class instances and arrays. [3] The lack of free functions is really only a minor inconvenience for language implementers. Clojure, for instance, just compiles new classes to hold functions as methods. From a programmer's perspective, you can write lambdas, but again, it's just syntactic sugar over an inner class. [1] https://www.happycoders.eu/java/main-method/ https://www.happycoders.eu/java/main-method/ [2] https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-4.html#jvms-4.1 https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-4.h... [3] https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-2.html#jvms-2.4 https://docs.oracle.com/javase/specs/jvms/se23/html/jvms-2.h...
- happymellon 1y agoThey are associated with a class, but the class is basically the file. There are limitations by doing it this way but it is out of preview and officially supported as of Java 25. https://openjdk.org/jeps/512 https://openjdk.org/jeps/512