manifold-systems / manifold

Manifold is a Java compiler plugin, its features include Metaprogramming, Properties, Extension Methods, Operator Overloading, Templates, a Preprocessor, and more.
http://manifold.systems/
Apache License 2.0
2.42k stars 125 forks source link

[Extension] Compilation failure when generic type name doesn't match. #575

Closed EotT123 closed 7 months ago

EotT123 commented 7 months ago

The following code doesn't compile.

package extensions.javax.swing.JList;

import javax.swing.*;

import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.Self;
import manifold.ext.rt.api.This;

@Extension
public class JListExt {

    public static <T> @Self JList<T> testing(@This JList<T> jList) {
        return jList;
    }
}

Whereas this compiles fine (notice the change in generic type names from T to E)

package extensions.javax.swing.JList;

import javax.swing.*;

import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.Self;
import manifold.ext.rt.api.This;

@Extension
public class JListExt {

    public static <E> @Self JList<E> testing(@This JList<E> jList) {
        return jList;
    }
}

Javac error:

javax/swing/JList.java:309: error: cannot find symbol
  public javax.swing. @manifold.ext.rt.api.Self() JList<T> testing() {
                                                        ^
  symbol:   class T
  location: class JList<E>
  where E is a type-variable:
    E extends Object declared in class JList

Apparently, because of the extending class uses E as the name, I'm forced to use the same name?

Desktop (please complete the following information):

rsmckinney commented 7 months ago

Yep, it’s an implementation shortcut. Doing the A+ solution where you define your own type var is over-the-top involved and imo not worth the trouble. shrug

Fwiw, this is documented in manifold-ext docs section on generics.

EotT123 commented 7 months ago

I'm sorry, I didn't saw that in the documentation. Thanks for your answer.