Java object destructuring

≡放荡痞女 提交于 2020-05-29 02:25:26

问题


In javascript there is object destructuring so we can break down objects and just use the end key if the intermidiate objects are resused multiple times. e.g)

const person = {
  firstName: "Bob",
  lastName: "Marley",
  city: "Space"
}

So instead of calling person.<> to get each value we can destructure it like this

console.log(person.firstName) 
console.log(person.lastName) 
console.log(person.city) 

Destructured:

const { firstName, lastName, city } = person;

And call like this:

console.log(firstName)
console.log(lastName)
console.log(city)

Is there something similar in Java? I have this Java Object that I need to get the value from and have to call long intermediate object names like this:

myOuterObject.getIntermediateObject().getThisSuperImportantGetter()
myOuterObject.getIntermediateObject().getThisSecondImportantGetter()
...

I would like this destructure it somehow and just call the last method getThisSuperImportantGetter(), getThisSecondImportantGetter() for cleaner code.


回答1:


As far as i know, java doesn't support this.

Other JVM language called Kotlin does support this

Kotlin | Destructuring Declarations




回答2:


Java Language Architect Brian Goetz has recently talked about adding destructuring to an upcoming version of Java. Look for the Sidebar: pattern matching chapter in his paper:

Towards Better Serialization

I strongly dislike the current proposal of the syntax, but according to Brian your use case will look like the following (please note, that at this point this is a proposal only and will not work with any current version of Java):

public class Person {
    private final String firstName, lastName, city;

    // Constructor
    public Person(String firstName, String lastName, String city) { 
        this.firstName = firstName;
        this.lastName = lastName;
        this.city = city;
    }

    // Deconstruction pattern
    public pattern Person(String firstName, String lastName, String city) { 
        firstName = this.firstName;
        lastName = this.lastName;
        city = this.city;
    }
}

You should than be able to use that deconstruction pattern for instance in an instanceof check like so:

if (o instanceof Person(var firstName, lastName, city)) {
   System.out.println(firstName);
   System.out.println(lastName);
   System.out.println(city);
}

Sorry, Brian does not mention any direct destructuring assignment in his examples, and I'm not sure if and how these will be supported.

On a side note: I do see the intended similarity to the constructor, but I personally do not like the current proposal that much, because the arguments of the "deconstructor" feel like out-parameters (Brian says as much in his paper). For me this is rather counter-intuitiv in a world where everybody is talking about immutability and making your method parameters final.

I would rather like to see Java jump over the fence and support multi-value return types instead. Something along the lines of:

    public (String firstName, String lastName, String city) deconstruct() { 
        return (this.firstName, this.lastName, this.city);
    }


来源:https://stackoverflow.com/questions/56586093/java-object-destructuring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!