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

manifold-ext: integrate automatic structural type checking into compiler & IJ plugin #577

Open rsmckinney opened 6 months ago

rsmckinney commented 6 months ago

Currently, a structural interface via @Structural requires a cast if not nominally implemented.

MyIface x = (MyIface) new MyClass(); // :(  go away cast

If MyClass satisfies MyIface structurally but not nominally, it still requires a cast. This was always a stopgap solution for true structural typing. Let's finally include structural type checking into the compiler and IDE plugin.

MyIface x = new MyClass(); // :) 
CharSequence hi = x.echo("hi");
double one = x.echo(1);

where:

@Structural
public interface MyIface {
  CharSequence echo(String s);
  double echo(int i);
}

public class MyClass {
  public String echo(CharSequence s) {
    return s.toString();
  }
  public int echo(double d) {
    return (int)d;
  }
}

Generics must be handled as well.

List<MyClass> list = ...;
foo(list); 
. . .
void foo(List<? extends MyIface> l) {
  . . .
}

Update:

In addition to satisfying by method, type checking should also support the other ways of satisfying a structural interface, such as by field, by proxy, etc. See criteria in documentation.

rsmckinney commented 6 months ago

Fix available with release 2023.1.13

rsmckinney commented 5 months ago

Reopened. See addendum in description.