arcadian

arcadian

Are there accessible metaprogramming examples?

I have been reading “Polished Ruby Programming” by Jeremy Evans and am really enjoying it. He has one chapter, chapter 9, on Metaprogramming, and another, chapter 10, on DSLs.

His examples are quite accessible, like for example he starts by creating a class where, when you call it with a missing method and argument to evaluate, it uses missing_method to actually create a method returning that result. So write myclass.foo 4 + 8, and from then on myclass.foo will evaluate to 12.

There is also another example - quite funny IMO - where he creates a module named “Rusty” where you can define functions with fn instead of def, and do it in one line. So within that module you can write fn :rand, “Time.now.usec/1000000.0”, and when you call myclass.rand you get a psuedo-random number based on the time fraction.

And another example where he uses method missing at the kernel level to give ruby an always accessible words(x y z) function that returns an array of those words as symbols, i.e. [:x :y :z], although admittedly that one looked like dark magic to me.

I was wondering whether elixir had any examples like this, since I know that the language was influenced by Ruby and has similar metaprogramming capabilities. Ultimately I would like to understand how modules that create their own DSLs function, like for example how Phoenix does its routing via the module in the router.ex file.

Most Liked

joey_the_snake

joey_the_snake

cloudytoday

cloudytoday

To add to the above, official guides on macros are decent for getting going.

See the very end of the side bar for the next two chapters.
You can also just google stuff, in my experience it’s quite accessible and sometimes can even be useful.
Then, official documentation will come very handy.

BartOtten

BartOtten

Addendum: Read the book before the Erlangelist posts. The former is a very gentle introduction and will show you many tecniques (for your examples). The latter goes deep really fast (but goes to (dark) places the book nevers goes) :slight_smile:

But yeah, those two are indeed all you need

kip

kip

ex_cldr Core Team

Metaprogramming in Elixir is more than macros. Arguably the non-macro parts are more accessible, easier to understand and less potentially obfuscating so I typically recommend starting there. And yes, Chris’s book is great (first Elixir book I bought way back when).

Its also a testament to José’s original language design that the AST hasn’t fundamentally changed. Some changes to the metadata to support improvements to the overall developer experience but that’s about it.

D4no0

D4no0

The Metaprogramming Elixir very much, I always tend to return to it when I forget how to do some shenanigans in my macros.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement