zerobuilder icon indicating copy to clipboard operation
zerobuilder copied to clipboard

Add support for parametrized types

Open jbgi opened this issue 9 years ago • 4 comments

Currently polymorphic target are not supported. I think the following:

@Goal(name = "entry")
  static <K, V> Map.Entry<K, V> entry(K key, V value) {
    return new AbstractMap.SimpleEntry<K, V>(key, value);
  }

should generate something like:

  [...]
  public static EntryBuilder.Key entryBuilder() {
    return INSTANCE.get().entryBuilderImpl;
  }
  private static final class EntryBuilderImpl implements EntryBuilder.Key,
                                                         EntryBuilder.Value<Object> {
    private Object key;
    EntryBuilderImpl() { }
    @Override
    public <K> EntryBuilder.Value<K> key(K key) {
      this.key = key;
      return (EntryBuilder.Value<K>) this;
    }
    @Override
    public <V> Map.Entry<Object, V> value(V value) {
      Object key = this.key;
      this.key = null;
      return Request.entry(key, value);
    }
  }
  public static final class EntryBuilder {
    private EntryBuilder() {
      throw new UnsupportedOperationException("no instances");
    }
    public interface Key {
      <K> Value<K> key(K key);
    }
    public interface Value<K> {
      <V> Map.Entry<K, V> value(V value);
    }
  }

if recycling is activated.

jbgi avatar Oct 23 '16 18:10 jbgi

Thanks @jbgi this looks natural. After modularisation of the API is complete, this will go in the builder module. It seems like the other module, updater, will not be affected.

h908714124 avatar Oct 23 '16 19:10 h908714124

First attempt: https://github.com/h908714124/zerobuilder/blob/9b235bdbd36ddfe8513c2075ebe962b2934b70b5/examples/basic/src/test/java/net/zerobuilder/examples/generics/MapEntryTest.java

recycle, @Step and non-static goal methods don't work yet.

h908714124 avatar Nov 12 '16 11:11 h908714124

Release 1.522 supports type variables. In goals with typevars, recycle is currently ignored, and update is forbidden.

h908714124 avatar Nov 16 '16 05:11 h908714124

1.523 finally also generates a generic updater, see GenericConstructor.java.

h908714124 avatar Nov 18 '16 06:11 h908714124