Other ways to use destructuring

P Holder pholder at gmail.com
Sat Apr 1 04:08:02 UTC 2023


In thinking about record destructuring it occurs to me it could be used in
more places than currently proposed.


One example would be in a function call:


record HumanName(firstName: String, middleName: String, lastName: String) {}

HumanName humanName = new HumanName(“Alice”, “Bateman”, “Chandler”);


String getFormattedName(humanName: HumanName(firstName, _, lastName))

{

   return lastName + “, “ + firstName;

}


final String fmtName = getFormattedName(humanName);



another could be in a for loop


final Set<HumanName> humans = new HashSet<>();

humans.add(humanName);

for (final humanName: HumanName(firstName, _, lastName): humans)

{

   processFirstName(firstName);

   processLastName(lastName);

   ...

}


or indeed in any body


with humanName as HumanName(firstName, _, lastName)

{

   processFirstName(firstName);

   processLastName(lastName);

   ...

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230401/98ff9b2a/attachment.htm>


More information about the amber-dev mailing list