#Are operators and keywords functions?

17 messages · Page 1 of 1 (latest)

north scaffold
#

I'm going through basics. I see operators are denoted with arity like +/2 and same for the keywords like if/2. Does this imply they are all functions under the hood?

tranquil bough
#

Yes

#

Well, many keywords are macros but if it's easier for the time being think of them as functions and it will make the most sense to you.

north scaffold
tranquil bough
north scaffold
#

Wow ok, meta-functions

tranquil bough
#

You can use them to build functions and otherwise inject code into the AST.

#

It's similar to macros in Lisp if you've ever done Lisp.

north scaffold
tranquil bough
#

I'm sure you've worked with some sort of framework that built html on the fly. That's what a macro can do with Elixir code.

north scaffold
#

Ooh I see, so it's code that writes code, and it mainly writes functions since Elixir is functional, that's exciting!

rugged hamlet
#

macros are functions that operate on AST

spare wyvern
#

you can check out https://astexplorer.net/ to get an idea of what ASTs look like in different languages, there's a Javascript AST generator here too.

#

In Elixir you can very easily get an AST by adding 'quote'/'unquote' around a block of code (it's really nice, like in JS you have to npm install a library to turn your JS code into AST)

#

a 'macro' in Elixir is like a function that takes in some Elixir code you wrote, uses 'quote'/'unquote' to turn that Elixir code into an AST, then reorganizes that AST and writes it back out as Elixir code

compact veldt