Skip to content

Generic typed objects cannot be used easily as optional parameters #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
EotT123 opened this issue Apr 19, 2025 · 1 comment
Open

Generic typed objects cannot be used easily as optional parameters #694

EotT123 opened this issue Apr 19, 2025 · 1 comment

Comments

@EotT123
Copy link
Contributor

EotT123 commented Apr 19, 2025

When trying to use generic typed objects (such as List<String>) as optional parameters, it is not possible to automatically infer the generic type. Instead, the type must be explicitly specified, otherwise, a type mismatch error occurs.

public class Test {

    //Incompatible types: 'List<Object>' cannot be converted to 'List<String>'
    public void test1(List<String> string = List.of()){ }

    //Incompatible types: 'ArrayList<Object>' cannot be converted to 'List<String>'
    public void test2(List<String> string = new ArrayList<>()){ }

    // OK
    public void test3(List<String> string = new ArrayList<String>()){ }
}

Is there a technical limitation that prevents generic types from being automatically inferred for optional parameters?

EotT123 pushed a commit to EotT123/manifold-test that referenced this issue Apr 19, 2025
@rsmckinney
Copy link
Member

Yep, this looks like an IntelliJ plugin issue. Default expression inference works fine with the compiler. Thanks for flagging this!

EotT123 pushed a commit to EotT123/manifold-test that referenced this issue May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants