top | item 28526848

(no title)

muhammedbash | 4 years ago

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.

discuss

order

carimura|4 years ago

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...

stickfigure|4 years ago

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();

muhammedbash|4 years ago

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.

Cojen|4 years ago

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.

MichaelMoser123|4 years ago

you can use lombok for generating accessors, helps to reduce a lot of boiler plate code. See https://javabydeveloper.com/project-lombok-tutorial/

muhammedbash|4 years ago

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.