admin管理员组

文章数量:1310466

I'm making a simple website with Yesod, and was trying to make my Julius more type-safe.

At the moment I have a JS function sendAction that sends a POST request with some JSON data to an endpoint defined with Yesod.

I've been using it like this:

sendAction({tag: "ClickMouse", button: "left"});

The action is Haskell looks like this:

data Action
  = ActionClickMouse MouseButton
  | ActionMoveMouse Int Int
  deriving (FromJSON) ...

data MouseButton = MouseButtonLeft | MouseButtonRight
  deriving ...

To make this more type-safe, I changed the Julius code to be:

sendAction('#{ActionClickMouse MouseButtonLeft}');

This works as expected (Note: when giving sendAction an object it json-stringifies it, when giving it a string it just sends it without conversion).

Now, for the mouse move action I have:

const x = some calculation
const y = some calculation

sendAction({tag: "MoveMouse", x, y});

However, I can't figure out how to make this similarly more type safe (or if that's possible at all really). As the following just results in the error Variable not in scope: x:

const x = some calculation
const y = some calculation

sendAction('#{ActionMoveMouse x y}');

Is there some way of using variables from JS in the Haskell interpolation?

本文标签: haskellHow to use JS variables in Julius interpolationStack Overflow