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

Last Post!

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.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement