News for hackers. Stuff that matters.
Re:Java 17 / JDK 17: General Availability by muhammedbash on Tuesday September 14, 2021 @12:07PM (#28526848)
Something I would like to see in Java is for functions to return multiple (named?) values. I think it will reduce boiler plate code considerably.
Re:Java 17 / JDK 17: General Availability by carimura on Tuesday September 14, 2021 @12:25PM (#28527120)
Part of the vision of pattern matching is aggregation and destructuring [1]. This is just exploratory but may be what you're thinking about?

[1] https://github.com/openjdk/amber-docs/blob/master/site/desig...

Re:Java 17 / JDK 17: General Availability by stickfigure on Tuesday September 14, 2021 @12:47PM (#28527433)
Interesting but the "Isn't this just multiple return?" section seems to completely miss the point. I don't see any examples of what multiple-return would look like. And multiple-return is what I want.

At first glance, it looks like this (sure, more powerful) pattern matching system is not going to satisfy my desire for a compact syntax that lets me do the equivalent of this typescript:

    const [foo, bar] = getMeTwoThings();
Re:Java 17 / JDK 17: General Availability by BlackFly on Tuesday September 14, 2021 @01:35PM (#28528037)
No, there is no tuple support yet in Java. That would require something like variadic generics or a special compile time syntax sugar for tuples as the sole use of variadic generics.

With the pattern matching the best you are going to get is nasty boiler plate like

    var returnedTuple = getMeTwoThings();
    if (returnedTuple instanceof MyTupleType(Foo foo, Bar bar)) {
        // Do something with foo and bar
    } else {
        // Not reachable unless refactoring, etc. So panic here.
    }
That else clause is so terrible you'd probably just rather use the getters from the type.
Re:Java 17 / JDK 17: General Availability by evacchi on Tuesday September 14, 2021 @03:11PM (#28529533)
I think there is ongoing discussion to support

      var MyTupleType(Foo foo, Bar bar) = getMeTwoThings();
which IIRC may be even simplified to

      var MyTupleType(foo, bar) = getMeTwoThings();
EDIT: found it https://mail.openjdk.java.net/pipermail/amber-spec-experts/2...

     let Point(var x, var y) = aPoint
Re:Java 17 / JDK 17: General Availability by titovodka on Tuesday September 14, 2021 @01:10PM (#28527715)
I might not be aware of best practices.

What are the benefits of having a multiple return type function?

I just assume that one can use an object if this is needed throughout the whole code..

Or maybe create an arraylist if possible and return that?

Re:Java 17 / JDK 17: General Availability by sverhagen on Tuesday September 14, 2021 @05:08PM (#28531253)
Using the object(/class) as you say is the probably "the Java way" to do this, given their historical "everything is an object" stance (which has been eroded a bit, though, with some of the functional support). But it sure is nice to do a multi-returns, here and there, without having to create another class. A class which required you to write boilerplate (getters/setters). Maybe records are some sort of compromise. Probably a non-compromise for people who like the multi-returns.
Re:Java 17 / JDK 17: General Availability by muhammedbash on Tuesday September 14, 2021 @12:32PM (#28527219)
I am thinking of something more like go language or python.

In go you can do this:

function (x, y int) (sum, prod int) { return x+y, x*y }

It makes a big difference when one is using the same input to generate multiple related outputs.

Re:Java 17 / JDK 17: General Availability by Cojen on Tuesday September 14, 2021 @01:25PM (#28527911)
I think the best thing we have is the new `record` feature. You can declare a small public record before the method with the return type, and by using the `var` keyword, the caller doesn't need to repeat the type declaration.
Re:Java 17 / JDK 17: General Availability by bgruber on Tuesday September 14, 2021 @06:57PM (#28532654)
Slated to be previewed in java 18 https://openjdk.java.net/jeps/405
Re:Java 17 / JDK 17: General Availability by MichaelMoser123 on Tuesday September 14, 2021 @12:16PM (#28526984)
you can use lombok for generating accessors, helps to reduce a lot of boiler plate code. See https://javabydeveloper.com/project-lombok-tutorial/
Re:Java 17 / JDK 17: General Availability by muhammedbash on Tuesday September 14, 2021 @12:38PM (#28527299)
I use the lombok plugin with IntelliJ, it eliminates tedious parts of some code.

Some times one needs to create new objects return multiple related items from a function. Or use a collection or array. I think it is unnecessary. Other languages (e.g. go or python) have had this for a while now. I taking a wild guess here LISP probably had it since the 1970s.