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.
Some APIs in Armeria provide both a unary method and a varargs method. (e.g. ServerListenerBuilder)
armeria/core/src/main/java/com/linecorp/armeria/server/ServerListenerBuilder.java
Lines 171 to 182 in e872d75
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:
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.