It's a poster child for a 'let' expression instead of twisting instanceof to work with no type return let results = multiResolve(false) in results.length == 1 && results[0].isValidResult() ? results[0].getElement() : null;
Rémi ----- Mail original ----- > De: "Brian Goetz" <brian.go...@oracle.com> > À: "Tagir Valeev" <amae...@gmail.com> > Cc: "amber-spec-experts" <amber-spec-experts@openjdk.java.net> > Envoyé: Lundi 4 Mars 2019 08:34:46 > Objet: Re: Patterns for arrays of specific length > In general, there’s a duality between the ways in which we construct > composites > and the way we deconstruct them. There’s an obvious duality between > constructor and deconstructor patterns; between static factories and static > patterns, etc. > > For structural composites, the obvious place to start is the dual of > array/list/map literals. (We’re not ready to do these just yet, but it makes > sense that we consider structural literals and structural patterns at the same > time.) So for example, if an array literal looks like [ e, f, g ], an array > pattern might look like [ p, q, r ]. > > Given that, your example might look like: > > return resolveResults instanceof [ var e ] && e.isValid() ? e : null > > Without collection literals, we’d write methods to do what we want, so these > methods have duals too. For example: > > return resolveResults instanceof oneElementArray(var e) && e.isvalid() ? e > : > null > > > > >> On Mar 4, 2019, at 3:30 AM, Tagir Valeev <amae...@gmail.com> wrote: >> >> Hello! >> >> In intellij IDEA code we often see snippets like this: >> >> final ResolveResult[] resolveResults = multiResolve(false); >> return resolveResults.length == 1 && resolveResults[0].isValidResult() ? >> resolveResults[0].getElement() : null; >> >> I wonder if special kind of patterns to cover such case could be invented >> like >> >> return multiResolve(false) instanceof ResolveResult[] {var res} && >> res.isValidResult() ? >> res.getElement() : null; >> >> In essence it should be a deconstruction pattern for arrays. I don't >> remember whether it was discussed, but probably I'm missing something. >> >> Alternatively this could be covered by utility method like >> >> static <T> T getOnlyElement(T[] array) { >> return array.length == 1 ? array[0] : null; >> } >> >> return getOnlyElement(multiResolve(false)) instanceof ResolveResult >> res && res.isValidResult() ? >> res.getElement() : null; >> >> But this doesn't scale for arrays of bigger length. >> >> With best regards, > > Tagir Valeev.