Lurker News

Comments (218) 56 threads

Show all comments

Showing thread 42 of 56

gwbas1c4 replies#
I don't understand the example. Does it even compile?

It's been a long time since I've used C, so maybe it's using some syntax that I'm unaware of?
IE: What defines "home" that is referenced as an argument to the "appRoute" function, and then passed to the "get" function to set up the "/home" route? Is "home" defined in lavandula.h, or is this really pseudocode?

diath#
It's a macro:

    #define appRoute(name) HttpResponse name(AppContext ctx)
ashtonjamesd2 replies#
Hi, sorry maybe I should've added a comment for that.

The 'appRoute' is a macro that expands to a function signature.
The macro is: '#define appRoute(name) HttpResponse name(AppContext ctx)' and the parameter I passed as 'home' is expanded into the function name. The reason is because all controller function signatures are the same, so just writing 'appRoute' allows the developer to save time writing endpoints!
It is a tradeoff between readability and development speed. And one of the ideas behind the framework is succint and minimal code.

gwbas1c#
So it creates a function called "home", and that is what you pass to get?

Makes sense, thanks!

nodesocket#
Thanks for explaining. I was also a bit confused at a first where the home variable being passed into get() was coming from.
hofrogs#
"appRoute(home)" is a macro that expands to a function called "home":

    #define appRoute(name) HttpResponse name(AppContext ctx)
rnhmjoj#
If I can guess, I would say `appRoute` is a macro that defines a struct called `home` with that handler being assigned to some field as a function pointer.