Chapter 2: Code from book page 34 has compile error

router.get("hello", String.parameter) { req in
        let name = try req.parameters.next(String.self)
        return "Hello, \(name)!"
}

The code above gives this compile error for router.get:

"Type ‘PathComponentsRepresentable’ does not conform to protocol ‘ExpressibleByStringLiteral’

How do we fix this. Thanks.

OK, I found the fix:

The book code is slightly different than the vapor generated code. Adding the return type (as the book does) will fix the issue.

router.get("hello", String.parameter) { req -> String in

2 Likes

@blakninja Thank you for sharing the solution - much appreciated! :]

Well worked out! The reason is because the Swift compiler has trouble inferring complex expressions (even though it’s obvious for us!)