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.

Reply via email to