← Back to context

Comment by gwbas1c

4 days ago

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?

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.

  • Thanks for explaining. I was also a bit confused at a first where the home variable being passed into get() was coming from.

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

    Makes sense, thanks!

"appRoute(home)" is a macro that expands to a function called "home":

    #define appRoute(name) HttpResponse name(AppContext ctx)

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.

It's a macro:

    #define appRoute(name) HttpResponse name(AppContext ctx)