Local vars as expected result
josephwilk opened this issue · comments
fact "local var" do
x = 10
10 |> x
end
Error.
** (CompileError) test/integration/t_scoping.exs:39: function x/1 undefined
Since the |>
is expecting a function on the right side the variable is called as if it was a function.
We should check the local bindings and see if we can resolve as a local rather than a fn. I have a vague memory this was very difficult previously due to determining the difference between a local var and a fun call. Will require some investigation.
Also see test here for details: https://github.com/josephwilk/amrita/blob/master/test/unit/amrita/elixir/t_pipeline.exs#L11
The best I have so far:
defp pipeline_op(left, { call, line, atom }) when is_atom(atom) do
quote do
local_var_value = binding[unquote(call)]
if local_var_value do
unquote(left) |> equals local_var_value
else
unquote(Macro.escape({call, line, [left]}))
end
end
end
Works BUT the else condition is invalid (its just a tuple rather than a function invocation.
Tricky since we only now at runtime that we should use a var rather than a function call. And since we have to be syntactically valid that function call has to exist if we unquote it...
Everything works except local function calls.
I used to think this was impossible.
But this pull request achieves it: https://github.com/josephwilk/amrita/pull/85/files
It does so at the cost of some uglyness in the |> functions.
Since this function is core to the entire framework I want to try and get this cleaner before the merge.