I have been thinking about this problem for some time. This is not a real world problem, I ask this for curiosity.
Is it possible to find out the value stored inside a closure in any programming language? See this JavaScript example. How can I find out what is the value stored inside that closure?
$ node
> const f = x => y => x * y
undefined
> const closure = f(8)
undefined
> closure(9)
72
>
Is it possible to use some metaprogramming magic in Elixir to find out the value of x
? This did not work:
$ iex
Erlang/OTP 21 [erts-10.3.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]
Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> f = quote do: (fn x -> fn y -> x * y end end).(8)
{{:., [],
[
{:fn, [],
[
{:->, [],
[
[{:x, [], Elixir}],
{:fn, [],
[
{:->, [],
[
[{:y, [], Elixir}],
{:*, [context: Elixir, import: Kernel],
[{:x, [], Elixir}, {:y, [], Elixir}]}
]}
]}
]}
]}
]}, [], '\b'}
iex(2)>
I found out that quoting functions is not possible in Elixir. Is this even possible in any programming language?