Downsides of Elixir Code Generation?

Are there downsides of generating elixir code from files? Any anti patterns known for this?

I used it to map chars in a string in my slugger library:

The Elixir String.Unicode Module uses a similar approach for handling Unicode data:

1 Like

Would the overhead of opening and closing a file be something worth keeping in mind?

The idea of “code generation” is to precompute stuff at compile time rather than runtime.
So instead of reading a lookup map from a JSON file each time, it can only be done once when compiling.


@h4cc have you done or are you thinking about doing a simple tutorial about Code Generation in Elixir? I think it’s very interesting and a possible solution to a problem I must solve in my project, so I’m really interested in learning this.

1 Like

I also use this technique in my Octet library

I think this solution could use power of pattern matching to optimize function call

@sashaafm you could read more about this technique in the book “Metaprogramming Elixir”, chapter 3


I’m not sure if you and @h4cc are doing the same thing? But what I was asking about was if is this a way to load code from files given a certain match.

Let’s say I have a system variable and if this value is A then lode code A if the var is B then load code B, BUT I if code A loads I do not want code B to be loaded and I don’t want it to be inside any of the project’s files. What I’m trying to do is akin to a Software Product Line. where I can make different versions of the same program without having the respective features/code of the other version.

Hope I explained that well enough :confused:

1 Like

If I do understand you correctly, there is done a very similar thing when using mix. Depending on your current setting of the environment variable MIX_ENV one of multiple config files is loaded. Something similar should also be possible for the project itself.

But I am not sure what you mean by “I don’t want it to be inside any of the project’s files”. If its not in the projects files, you can’t choose it among others at compile time.


@NobbZ, what I’m trying to do is pretty much Aspect-Oriented Programming in Elixir. If you have ever used AspectJ, that was the functionality I was trying to explain.