NeoForge icon indicating copy to clipboard operation
NeoForge copied to clipboard

Adding providers to `DataGenerator` via lambda is ambiguous

Open rlnt opened this issue 1 year ago • 5 comments

Minecraft Version: 1.20.4 NeoForge Version: 20.4.232 Logs: https://gist.github.com/rlnt/2866adea220d3f8e80aa6771a8357569

Steps to Reproduce:

  1. Create a class that extends the NeoForge LanguageProvider
  2. Create a method where you have the GatherDataEvent as parameter
  3. Get the generator from the event via event.getGenerator()
  4. Call the addProvider method on the generator that uses the DataProvider.Factory<T>

Description of issue: You can't compile the code because the call is ambiguous.

Edit: code snippets because the log is a bit odd

public final class LazierLang extends LanguageProvider {

    LazierLang(PackOutput output) {
        super(output, BuildConfig.MOD_ID, "en_us");
    }

    @Override
    protected void addTranslations() {
    }
}
public final class DataGeneration {

    private DataGeneration() {}

    public static void init(GatherDataEvent event) {
        DataGenerator generator = event.getGenerator();
        generator.addProvider(event.includeClient(), LazierLang::new);
    }
}

rlnt avatar Apr 20 '24 18:04 rlnt

Don't you need a new operator to create an instance of your data provider?

KnightMiner avatar Apr 20 '24 19:04 KnightMiner

He means:

generator.addProvider(true, Provider::new)

javac complains that this is ambiguous

shartte avatar Apr 20 '24 19:04 shartte

Is this just poor method resolution on javac's side? The two method signatures are

public <T extends DataProvider> T addProvider(boolean run, DataProvider.Factory<T> factory);
public <T extends DataProvider> T addProvider(boolean run, T provider);

There's no question that T::new should not bind to T

Shadows-of-Fire avatar May 03 '24 04:05 Shadows-of-Fire

I think the problem is both take the same parameter (constructor and the datagen run method), just differ in return types.

KnightMiner avatar May 03 '24 17:05 KnightMiner

I assumed that it may be ambiguous due to T referring to the DataProvider interface which only has one method and is then interpreted as a functional interface as well. The @FunctionalInterface annotation in Java is optional and just an indicator.

rlnt avatar May 03 '24 18:05 rlnt