# Other languages generating code like Elixir does

**URL:** <https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908>\
**Category:** General Prog (Archived)\
**Tags:** metaprogramming\
**Created:** [November 14, 2018, 11:02am UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908 "2018-11-14T11:02:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 11:02am UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/1 "2018-11-14T11:02:44Z")

</div>

One thing I really like in Elixir is the possibility to generate Elixir code in Elixir **without using even macros**.

Do you know of any other language with this same capability? For example, such language could be an interesting choice to create satellite nodes of an Elixir/Erlang system.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [November 14, 2018, 11:56am UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/2 "2018-11-14T11:56:00Z")

</div>

- Ruby where you have for example `test` which is plain function that creates function.
- Lisps, where code is data.
- JavaScript where you can dynamically create structures and add methods to the prototypes. Io in the same manner.
- AFAIK Python can do that (I have never worked with Python though), for examples see `import future`
- Can we count JIT there? If yes, then almost all compiled languages can use form of the “on the fly” code generation.
- Java (annotations) and C# - via reflection
- C++ templates are Turing complete
- D templates are quite powerful, however some can count that as a cheating, as these are almost macros in Elixir sense
- Lua - metatables requires that

Your question is quite broad, as in theory, you can generate any code in any language (as long as both are Turing complete) and compile/interpret it as long as you are able to run compiler/interpreter, which you are able to do if you can run Turing complete code on that machine (just implement compiler/interpreter in language of your choice).

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:01pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/3 "2018-11-14T12:01:34Z")

</div>

Thanks for your detailed response.

Indeed, any language can be used to generate code in any other language, if not by writting a big string to a file and invoking the compiler of the generated source.

But my question is which other languages do so as elegantly as Elixir does.

But let me reduce the scope a little: and which are not Lisps.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [November 14, 2018, 12:11pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/4 "2018-11-14T12:11:24Z")

</div>

“Elegance” is highly subjective. So still it is hard to tell. We can even argue that Ruby meta programming is “cleaner” as do not require `quote`/`unquote` magic. Or that D templates are much better solution, as it almost looks like “regular” code. Or that Rust attributes syntax is much cleaner for end-user together with explicit marks when you use macro (all macro invocations need to end with `!`).

Also even with Elixir macros you cannot change language syntax, what is possible in some other languages (like Racket or Rust, as long as it is parseable).

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:19pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/5 "2018-11-14T12:19:56Z")

</div>

So lets refine my question again.

What I mean by “elegance” is “not using strings”, but instead, using the syntax of the language to generate its code, like in this example

```
defmodule Example do
    kv = [foo: "called foo", bar: "called bar"]
    Enum.each kv, fn {k, v} ->
      def unquote(k)(), do: unquote(v)
    end
end

```

Note that we are **not using macros here in the sense that we are not generating code inside a defmacro block**.

Which other languages can generate their own code like this, and which are not Lisp?

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [November 14, 2018, 12:21pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/6 "2018-11-14T12:21:20Z")

</div>

`def` is a macro. The usage of `unquote` kinda give it away …

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:24pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/7 "2018-11-14T12:24:05Z")

</div>

Nice one, but consider that it is fully expanded to remove all macros, and then we get pure Elixir code that generates Elixir code, so I think my question still holds.

Or does the “pure Elixir” code the expansion results into, uses strings to build the generated code?

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [November 14, 2018, 12:25pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/8 "2018-11-14T12:25:03Z")

</div>

What you get is a data structure representing elixir code that is then used to “generate” erlang code …

It’s like writing an interpreter for lisp in any language, I recon.

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:28pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/9 "2018-11-14T12:28:16Z")

</div>

Ok, no strings, so its Elixir generating Erlang code …

See that I refined my question above to “Note that we are **not using macros here in the sense that we are not generating code inside a defmacro block**.”

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [November 14, 2018, 12:28pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/10 "2018-11-14T12:28:58Z")

</div>

elixir compiler generates the code, not elixir itself. We are using macros even if they are not explicit as is the case with `defmacro`. `defmodule`, `def`, etc are all macros. elixir expands them at compile time and then elixir’s compiler translates them into erlang.

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:30pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/11 "2018-11-14T12:30:14Z")

</div>

So the Elixir compiler takes the data structure you mentioned before, and traverses it to generate Erlang code? Is that so?

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [November 14, 2018, 12:30pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/12 "2018-11-14T12:30:37Z")

</div>

That’s my understanding, yes. I used to think that elixir targeted a layer below that (core erlang), but it seems to just output erlang.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [November 14, 2018, 12:31pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/13 "2018-11-14T12:31:02Z")

</div>

If you look closely, you will see macro there though 😉

But ok, lets see:

- [JavaScript](https://ideone.com/y5id5S)

```js
function Example() {
  let ret = {}
  const kv = {
    foo: "called foo",
    bar: "called bar"
  }

  for (let name in kv) {
    ret[name] = () => kv[name]
  }
  
  return ret
}

const example = Example()

console.log(example.foo())
console.log(example.bar())

```

- [Ruby](https://ideone.com/iKSGWY)

```ruby
class Example
  {foo: "called foo", bar: "called bar"}.each do |key, value|
    define_method(key) { value }
  end
end

puts Example.new().foo
puts Example.new().bar

```

Unfortunately I do not have time to write more examples right now.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [November 14, 2018, 12:34pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/14 "2018-11-14T12:34:19Z")

</div>

This “data structure” is called [abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) and it is exactly what `quote` will spit out. So this is tuple in form `{element :: atom(), environment :: keyword(), arguments :: term()}` (however I do not know how the root of the tree looks like, so maybe someone with more knowledge can say more.

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:34pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/15 "2018-11-14T12:34:53Z")

</div>

Thanks for your examples, they are enough to show what Javascript and Ruby can do with respect to code generation.

This seems very much like what Elixir can do.

Do you know if Rust, or any other statically typed language that can do the same, as you only gave examples in dynamic languages?

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [November 14, 2018, 12:37pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/16 "2018-11-14T12:37:53Z")

</div>

> [@mguimas](#):
>
> Which other languages can generate their own code like this, and which are **not Lisp**?

> [@Anonymous functions, the dot, and parameters](https://forum.elixirforum.com/t/anonymous-functions-the-dot-and-parameters/10886/7):
>
> The main reason is that Elixir is a lisp-2 language

> [@Anonymous functions, the dot, and parameters](https://forum.elixirforum.com/t/anonymous-functions-the-dot-and-parameters/10886/7):
>
> The main reason is that Elixir is a lisp-2 language - this means that variables and functions “occupy” different name spaces, so calling a function in the functions namespace and calling a function in the variables namespace has to work differently. Most languages are lisp-1 (which means that variables and functions are in the same namespace). Some other lisp-2 languages are Ruby, Common Lisp and Perl. Being lisp-2 means that this code is valid: def foo(arg), do: arg def bar() do foo = 1 …

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:40pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/17 "2018-11-14T12:40:08Z")

</div>

Well Elixir may expand everything to a big data structure, the AST, but then the part of the code corresponding to

```elixir
    kv = [foo: "called foo", bar: "called bar"]
    Enum.each kv, fn {k, v} ->
      ...
    end

```

must be executed some how so that it generates `foo` and `bar`.

This makes Elixir compiler behave somewhat differently to a typical compiler that picks an AST and visits its node to translate into the target code.

What does Elixir do differently?

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [November 14, 2018, 12:41pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/18 "2018-11-14T12:41:19Z")

</div>

elixir evaluates what it sees first unless it’s a macro, and then builds an ast from it.

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:42pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/19 "2018-11-14T12:42:57Z")

</div>

So there is initially a macro expansion phase, then Elixir executes the whole output of this phase, then it picks the resulting AST, and does the normal visit to translate to Erlang code. Is it like this?

---

<div class="post-metadata">

**Author:** ![mguimas](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mguimas/32/11229_2.png) [@mguimas](https://forum.elixirforum.com/u/mguimas)\
**Post date:** [November 14, 2018, 12:44pm UTC](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908/20 "2018-11-14T12:44:43Z")

</div>

Thanks for making me aware that Elixir is a lisp-2.

[Next page](https://forum.elixirforum.com/t/other-languages-generating-code-like-elixir-does/17908.md?page=2)
