projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.83k stars 2.37k forks source link

[FEATURE] Can it provide functions similar to js destructuring and assignment? I think this is very meaningful. #3501

Closed 1057105012 closed 1 year ago

1057105012 commented 1 year ago

When we write js code, we will notice the following code

let order = {
    name: "book"
    price: "22.3"
}
patchBook({book: "11", price: "22"})

Lombok can simulate this behavior at the Java level. Using idea support, I believe many people need it!

juliojgd commented 1 year ago

Does that code make any sense at all?

let order = {
    name: "book"
    price: "22.3"
}
patchBook({book: "11", price: "22"})

To assign a value to something that is not even a field. The field is "name", not "book". If that makes sense -> "I don't want to live in this javascript world anymore." Dr'. Hubert EcmasWorth.

1057105012 commented 1 year ago

I'm just giving an example. In fact, what I want to express is whether Java can be more free in constructing objects and operating assignments. My example may not be very expressive, but you should be able to understand what I want to express. What I want to express is that Java can use lombok to allow these writing methods when writing code, and there is no need to create a hashMap to do this kind of thing. One use is that when passing context across threads, you no longer need to use map.get("foo"), but can directly use ctx.foo like this. The main reason is that it will be much more convenient to write code to support this feature, and it is often encountered in Java. The operation of patching objects, and sometimes even having to construct some iterative classes that are not very useful

var foo = { foo: 'aaa', bar: 'bbb', count: 33 };
dstango commented 1 year ago

So what you want is something like "autoboxing/unboxing" for maps? (btw: you might like to revise your examples, as they are confusing, because they seem to be syntactically inconsistent/incorrect -- that makes it hard to understand your point IMHO)

I can't say if the Lombok developers would be willing to include such a feature, but one technical limitation that would prevent it might be that Lombok joins in with the compiler and modifies the syntax tree. If I'm not wrong that means the java source code has already been parsed and been organized into tokens. If that's the case all extras Lombok can offer are constrained by the general Java grammar. I fear there's nothing in the Java grammar that could parse {foo: "bar"}, so that would be a dead end, and thus your feature request would be impossible to realize within the boundaries of Lombok's approach.

But I don't know enough about Lombok's internals, so someone more knowledgeable hopefully can bring some enlightenment into this.

rzwitserloot commented 1 year ago

This is so far removed from what java can do, the answer is an empathic no and I'm shutting the debate down. If you want to discuss what this would look like, feel free. But not in an issue on this project.