arcadian

arcadian

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.

Showing Posts 1 to 9

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

Sebb

Sebb

the book is 8 years old, is it still valid?

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.

joey_the_snake

joey_the_snake

I’m not sure what you mean by valid. If you mean completely up to date with any new things they added since then, then no. If you want to know the fundamentals then yes.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Off the top of my head I can’t really think of anything that has been added on the macro front honestly.

al2o3cr

al2o3cr

I could see books that predate Code.quoted_to_algebra having to spend a lot of effort making AST → code transformations, but that’s more “macro-adjacent” than macro-related.

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews