intellij-postfix-templates icon indicating copy to clipboard operation
intellij-postfix-templates copied to clipboard

Support some simple generic constraints

Open alshain opened this issue 6 years ago • 4 comments

It would be great if I could do something like:

java.lang.Collection<? extends Car> -> CarUtils.shredAll($expr$);

Is something like this on the radar?

alshain avatar Aug 01 '18 09:08 alshain

So far I do not have an important use case for a special treatment of generics. I mean, in theory your extension proposal makes fully sense. But it is also quite some effort to implement this. Do you also have a real world example where you would actually apply this feature?

xylo avatar Aug 02 '18 07:08 xylo

@xylo Sure.

I have several methods/expressions that work on collections of Entities for example, or Collections of other things.

e.g. a method like: Entities.mapById(Collection<T extends Entity>): Map<Integer, T>

And a postfix template: students.mapById would make sense, but it wouldn't be applicable to a collection of strings.

I wouldn't want .mapById to appear on all lists and collections.

Since I would like to create a couple of postfix templates that are only applicable to collections containing elements of certain types, the postfix completions would not be applicable most of the times and pollute the completion list.

Does that make sense?

alshain avatar Aug 17 '18 14:08 alshain

Ah, I see. Originally, I thought you wanted to use multiple of those generics in one template. But you're right, the pollution of the completion list is indeed also an unpleasant matter. And I don't have any solution for that at the moment.

I did a first look what has to be replaced for implementing this, but it looks complicated. At the moment I use a high-level function from IDEA to determine if a A is a subtype of B. This function is even string based and not working with generics at all. So there are 2 things to do to implement this:

  1. Extend the language for defining postfix templates so that generics can be used here.
  2. Replace the high-level function for checking the sub-type property.

Both will take quite some time. So I put this feature request to my list of things to be done after releasing version 2.0.

xylo avatar Aug 19 '18 21:08 xylo

Thanks for the detailed reply!

I have raised a feature request with JetBrains for this. Maybe support in the built in postfix templates would make implementation here easier, too.

alshain avatar Sep 06 '18 09:09 alshain