Skip to content

Remove a unary method which gets overloaded by a varargs method #3384

Description

@ikhoon

Some APIs in Armeria provide both a unary method and a varargs method. (e.g. ServerListenerBuilder)

public ServerListenerBuilder whenStarted(Consumer<? super Server> consumer) {
serverStartedCallbacks.add(requireNonNull(consumer, "consumer"));
return this;
}
/**
* Adds {@link Consumer}s invoked when the {@link Server} is started.
*
* @see ServerListener#serverStarted(Server)
*/
@SafeVarargs
public final ServerListenerBuilder whenStarted(Consumer<? super Server>... consumers) {

That is a good pattern to avoid creating an additional single-sized array for a single parameter. However, some JVM languages such as Scala 2.12 cannot properly infer the input type when a lambda expression is used.
For example:

ServerListener.builder()
              // failed to compile with 'missing parameter type'
              .whenStarted { server => ... } 
              .build()

If we remove whenStarted(Consumer<? super Server> consumer) from the overloaded methods, the compiler correctly infers the type parameter for the lambda expression.
whenStarted() rarely called in an application life cycle, so the overhead is almost zero.
And we can apply this approach to other non-frequently called methods.

As this is breaking changes, this issue could be resolved when Armeria 2.0 is released.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions