ocsigen / eliom

Multi-tier framework for programming web and mobile applications in OCaml.
http://eliom.org
Other
305 stars 54 forks source link

Make client -> server RPC a little easier #473

Open bluddy opened 7 years ago

bluddy commented 7 years ago

It would be nice if the client stubs for client->server RPC was generated automatically. Rather than needing a client stub with derive.json, we could just define a server function (or %clientserver or some such thing) that would also create the client stub. This function definition would include the required derive.json. It could even convert multiple arguments to a tuple automatically.

Drup commented 7 years ago

I'm not sure I follow what that would involve. How would you create the json derived object that describe the return type of the rpc handler ?

Can you give a concrete code sample of what you want and what it desugars too ?

bluddy commented 7 years ago

Sure. Currently, I have to write:

let%server toggle_button (name, button, state) = ...
let%client toggle_button =
  ~%(Eliom_client.server_function [%derive.json: string * user_vars * bool] toggle_button)

(BTW, let me know if there's a more efficient way to do client->server RPC. I'm just learning.)

What I would like to do is this:

let%clientserver toggle_button [%derive.json: string * user_vars * bool] (name, button, state) = ...

Or even

let%clientserver toggle_button [%derive.json: string; user_vars; bool] name button state = ...

Where the tuplification will happen automatically on the sending and receiving end. But that's an extra wish item. The main one is to get rid of the let%client which is pretty much routine code.

Drup commented 7 years ago

Ah, I see.

I'm not sure I really want yet another syntactic sugar in eliom, especially for such a minor code shortening. On the other hand, this could perfectly well be an independent ppx package (ppx_eliom_rpc ?)

Also, you might as well reuse usual type annotations:

let%eliom.rpc toggle_button (name:string) (button:users_vars) (state:bool) = ...
bluddy commented 7 years ago

That's great! I really like that. I think the closer you can get the syntax to regular function calls, the better. Maybe when I have some time.