Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

A library author wants to expose curried APIs such as Function<Config, Function<Request, Response>> in a shared framework. What would you advise?

I would advise against exposing nested function types such as Function<Config, Function<Request, Response>> in a shared framework. They are hard to read, document and evolve in Java: adding a parameter changes the type everywhere and breaks every user, and stack traces and IDE support are weaker for anonymous function chains. A named interface such as Handler with a factory method expresses the same idea clearly and can grow compatibly, so currying can stay inside the framework where it helps, while the public API uses named types.

Why this question is asked

Library authors with a functional background often reach for curried signatures, because they capture a real idea elegantly: configure something once, then use it many times. In a shared framework, though, the signature is read by every user, documented once, and must survive years of change. Interviewers ask this design question to see whether we can weigh elegance against readability and compatibility at scale. In this answer, we will look at what the curried API expresses, see the problems with reading, evolving and debugging it, design a named alternative, and decide where currying can still be used.

1What the Curried Signature Expresses

Function<Config, Function<Request, Response>> says: give me a configuration, and I will give you something that turns requests into responses. That is a sensible design. Configuration happens once at startup, and the returned function handles many requests, with the configuration captured inside it.

Java
public final class Framework {
public static void register(String path, Function<Config, Function<Request, Response>> handler) { ... }
}
Framework.register("/orders", config -> {
OrderClient client = new OrderClient(config.baseUrl()); // runs once at startup
return request -> client.handle(request); // runs for every request
});

Notice that the idea itself is good: expensive setup is separated from per-request work. The question is not whether to have two phases, but how to express them in a public API that twenty teams will read and depend on.

2Hard to Read and Document

Nested generic types carry no names. A developer reading the register method must decode Function<Config, Function<Request, Response>> from the inside out, and the Javadoc has no natural place to explain "the outer function is called once at startup, the inner one per request, and it must be thread-safe". IDE autocompletion offers apply, which tells the user nothing about which phase they are in.

It gets worse as the framework grows. If handlers also need an asynchronous variant, the types become Function<Config, Function<Request, CompletableFuture<Response>>>, and signatures across the framework fill up with brackets that hide the domain concepts the framework is supposed to provide.

3Hard to Evolve

The biggest problem is change. Suppose the framework later needs to give handlers access to metrics. With a curried type, the only place to put a new input is another layer in the chain.

Java
// Version 1
register(String path, Function<Config, Function<Request, Response>> handler)
// Version 2: metrics added
register(String path, Function<Config, Function<Metrics, Function<Request, Response>>> handler)

Every handler in every team now fails to compile, because the type of the parameter changed. The old signature cannot be kept with a default either, because a Function has no room for extra methods or defaults; its only method is apply. Each such change becomes a breaking release, so the framework either freezes or forces migrations on all its users.

4Weaker Stack Traces and Tooling

When a handler fails, the stack trace shows frames such as lambda$main$0 and lambda$main$1 inside Framework internals, not OrdersHandler.handle. Profilers, debuggers and logging show the same anonymous names, so an operator looking at a slow request cannot easily tell which team's handler is responsible.

IDE support is weaker too. "Find implementations" does not work for a Function in the same way as for a named interface, since every lambda of the same shape anywhere in the codebase matches, and "find usages" of apply returns thousands of results. A named type gives every tool something specific to search for.

5The Named Alternative

The same two-phase idea is clearer with two named interfaces, each with one method and documentation of its contract. Extra inputs go into a context object that can grow without breaking anyone.

Java
/** Handles requests for one path. Called concurrently; implementations must be thread-safe. */
@FunctionalInterface
public interface Handler {
Response handle(Request request);
}
/** Creates a Handler once at startup. */
@FunctionalInterface
public interface HandlerFactory {
Handler create(HandlerContext context);
}
public interface HandlerContext {
Config config();
default Metrics metrics() { return Metrics.NOOP; } // added in 2.1 without breaking anyone
}
Framework.register("/orders", ctx -> {
OrderClient client = new OrderClient(ctx.config().baseUrl());
return request -> client.handle(request); // still a lambda if users want one
});
Curried function type versus named interfaces
ConcernFunction<Config, Function<..>>HandlerFactory + Handler
Reading the signatureDecode nested genericsNames say what each phase is
Documenting contractsNo natural placeJavadoc on each interface
Adding an input (metrics)New layer: breaks every userNew context method with default
Stack traceslambda$main$1OrdersHandler.handle (if a class)
Writing a handlerLambdaLambda or class

Because both are functional interfaces, users can still write the same lambdas as before, so nothing is lost in brevity. But the phases now have names in the API, the Javadoc says what each one guarantees, and new inputs arrive as methods on HandlerContext, often with defaults, so existing handlers keep compiling. Teams that prefer classes can write class OrdersHandler implements Handler, which then appears by name in stack traces.

6Where Currying Still Fits

None of this means currying is useless. Inside the framework, where one team owns the code and nothing is published, a curried helper or a partially applied function can be a concise way to wire things together, and changes stay local. The rule I would give the author is simple: use currying internally if it helps, but expose named types publicly.

It also helps to frame the advice positively. The author's design instinct, separating configuration from per-request work, is exactly right, and the named interfaces keep it. Only the way it is spelled in the public API changes, which makes the review about compatibility and readability rather than about style preferences.

7Follow-up questions

Isn't a named interface just more boilerplate?

Two small interfaces of a few lines each, written once in the framework. Users write exactly the same lambdas as before. The cost is paid once by the author; the benefit, readable signatures and compatible evolution, is collected by every user for years.

Why put new inputs in a context object instead of adding method parameters?

Adding a parameter to create would change its signature and break every implementation. A context object can gain methods, with default implementations, without breaking anyone, so it is the standard way to keep a callback API open for growth.

Would Kotlin users mind the change?

No. Kotlin supports SAM conversion for Java functional interfaces, so Kotlin users can pass lambdas to register just as easily, and named interfaces are actually easier to read from Kotlin than deeply nested Function types.

8Conclusion

In this answer, we saw that a curried API such as Function<Config, Function<Request, Response>> captures a good idea, configure once and handle many requests, but in a shared Java framework it is hard to read and document, breaks every user when a parameter is added, and gives weaker stack traces and tooling. Named functional interfaces such as HandlerFactory and Handler, with a growable context object, keep the same idea and the same lambda syntax while making the API clear and able to evolve compatibly. Currying can remain an internal technique.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd tell the author the instinct is right but the spelling isn't. Function of Config to Function of Request to Response expresses a good idea: configure once at startup, then handle many requests with the configuration captured.

Then, if they want more

But in a shared Java framework it causes three problems. It's hard to read and document: nested generics have no names, there's no natural place in the Javadoc to say the outer function runs once and the inner one must be thread-safe, and the IDE just offers apply.

It's hard to evolve: if we later need to give handlers metrics, the only place to put it is another layer in the chain, which changes the parameter type and breaks every handler in every team, and a Function can't get default methods.

And debugging is worse: stack traces and profilers show lambda dollar names inside framework internals instead of the team's handler. My suggestion is two named functional interfaces, HandlerFactory with create taking a HandlerContext, and Handler with handle taking a Request, each documented.

Users can still pass exactly the same lambdas, but new inputs become methods on the context with defaults, so they don't break anyone, and teams can write named handler classes that show up in traces. Inside the framework, currying is fine if it helps; the rule is just that public signatures use named types.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A library author wants to expose curried APIs such as Function<Config, Function<Request...": https://scrivio-interview-library.netlify.app/java/functional-design/9327c39da6/