Generic parameter 'D' could not be inferred

I've written the following code:

...
    public static func execute<T: Decodable, D: TopLevelDecoder>(command: some ShellCommand, decoder: D = JSONDecoder()) throws -> T where D.Input == Data {
        ...
    }
...

But when I call it, I got the following error(I have already set the default value for the decoder parameter):

Generic parameter 'D' could not be inferred

You're not alone in wanting to do this, but prior to Swift 5.7, Swift cannot infer a type from a default parameter.

If I have understood the proposal correctly (I did not read the entire thing), a Swift Evolution proposal to do this was accepted in April 2022, and implemented in Swift 5.7:

The alternative in the meantime is to implement a helper function which omits the default argument:

public static func execute<T: Decodable>(command: some ShellCommand) throws -> T { ... }

From this, call your original function and pass it your default parameter. Not ideal, but works.

Thanks for your reply, I got it.