Patterns for arrays of specific length

Tagir Valeev amaembo at gmail.com
Mon Mar 4 09:13:40 UTC 2019


Hello!

> Arrays.stream(resolveResults).findFirst().filter(ResolveResult::isValidResult).map(ResolveResult::getElement).orElse(null)

This is obviously wrong: we need to return non-null result only if
there's exactly one resolve result. As IDE we need to support
incorrect code, in particular where reference resolves to several
symbols (e.g. ambigous method overload), but in most of the places we
want to proceed further only if the resolve result points to exactly
one symbol.

We could use a third-party collector, like in my StreamEx lib:
StreamEx.of(resolveResults).collect(MoreCollectors.onlyOne()).filter(ResolveResult::isValidResult).map(ResolveResult::getElement).orElse(null)

That would be technically correct, but I really worry about amount of
garbage created in such kind of code (the same concern applies to your
version as well). For us GC pressure is very important (you may
imagine amount of reports "IDEA eats insane amount of memory", "IDEA
stuck in garbage collection" and good old "IDEA is slow" we receive)
and when allocation-free version of the code is not much longer, I
would certainly prefer it.

With best regards,
Tagir Valeev.

>
> and obviously, the method should return an Optional instead of calling orElse(null) at the end.
>
> Rémi
>
> >
> > 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.


More information about the amber-spec-experts mailing list