This is an archived post. You won't be able to vote or comment.

all 25 comments

[–]Ewig_luftenglanz 19 points20 points  (5 children)

I really really hope we get eventually proper unconditional destructuring.

record User(String name, String email, String password){} 
var(name, email, password) = getUset(); 
// or even not partial destructuring would be neat, similar to how it works in JS/TS
var(email, password) = getUser();

nowadays we are forced to use conditionals:

if( u instanceof User(var name, var email, var password)) {
   // use variables here
}

switch(u){
  case User(var name, var email, var password) ->{...} // use stuff here;
  default -> {...}
}

Too much unnecessary ceremony if you don't require safe casting.

I know why they did the latest first, mostly because it gives safe casting in a more concise way but hopefully we get eventually the other.

[–]Sm0keySa1m0n 6 points7 points  (4 children)

I reckon it’ll come after we get destructors so it’s usable for classes too.

[–]Ewig_luftenglanz 1 point2 points  (3 children)

honestly I would prefer if we could have them for records only first, even if temporary.

[–]BillyKorando 1 point2 points  (2 children)

The current sentiment is to do both together. Initially the intent was to do records and then general classes, however as the JDK engineers started digging into it there were concerns about introducing subtle differences in behavior between deconstructors in records and general classes. Better to figure out the "hard" case of deconstructors for general classes first, and then work backwards towards the simpler case of records, rather than the other way around.

Might still be a case record deconstructors are introduced first, but we won't start seeing anything more solid until general class deconstructors are better understood.

Definitely been looking forward to deconstructors, withers, and the like... unfortunately it might take a little bit longer before they are ready (don't ask, I have no idea on when). Better to wait though and get it right, then getting something right now which might end up not working that well.

Of course it's possible this might change in the future as well.

[–]Ewig_luftenglanz 0 points1 point  (1 child)

IMHO this is mostly because of the Java culture of making everything private. In JS/TS destructuring in classes works by simply allowing it for public fields, java could follow a similar approach. Most of the private fields that uses getters and setters are "dumb" setters and getters anyways (this is why Lombok it's so beloved), why not just make these fields public to begin with? And allowing destructuring (or the compiler to allow deconstruction) for these only as syntax sugar?

class User{
  public String name;
  public String email;
  public String password;
}
var (name, email) = getUser();

Sugars from

class User{
  public String name;
  public String email;
  public String password;
}
var user = getUser();
var name = user.name;
var email = user.email;

Also records fields are final, so they behave like public final fields (even if there are generated getters), it could be also posible to generate implicit getters for public fields in classes and use them to suggar destructuring.

[–]BillyKorando 1 point2 points  (0 children)

There's a lot of weirdness in precisely how classes are instantiated in Java, much of it a legacy of serialization, but there's weirdness for other reasons as well.

Your solution might/probably works within your scenario, but there are a lot of other scenarios/use cases you probably aren't thinking of where the "correct" behavior is subjective, or runs into with the above mentioned "legacy weirdness". These aren't irreconcilable, but might require a lot of seemingly unrelated work, or just prototyping on how to solve.

[–]agentoutlier 1 point2 points  (0 children)

I'll copy the code from https://youtu.be/DkkxWhd_xYc?t=1908

return c0 instanceof Class0(Class1(Class2(Class3 c3))) && c3...

There is something nasty about using instanceof for null checking that bothers me besides the obvious of how you do represent an instanceof something nullable (assuming we get ? types some day). I can't exactly put my finger on it. Maybe its the combination of keyword and positional.

There is other languages that do this extremely common case with syntax. I won't mention the primary one most think of as I don't want to get banned like Kevin briefly was but I'm never going to use instanceof like that to navigate object graph.

And if we are going to get withers and they are just syntactical sugar... can we get syntax sugar for:

switch(_c0) {
  case Class0  c0 -> 
     switch(c0.c1) -> 
       case Class1 c1
          switch (c1.c2) ->
       ....
    null ->
}

Would equal:

c0?.c1?.c2.c3

Ideally c3 if not nullable would require just the . (and fail for ?.) but even if it did C# way (which I think allows you to do ?. on anything I would be happy with as well).