Nicoslav

Nicoslav

Reflection in Elixir

Which are the most common ways to do Reflection in Elixir? Especially Object-Analysis and Class-Analysis would be nice to know since there isn’t really anything available in the internet towards this topic.
Thank you guys in advance!

Most Liked

lukaszsamson

lukaszsamson

ElixirLS Core Team

There are some tools and techniques that allow to do things comparable to what Java/.NET reflection APIs provide.

  • for inspecting current loaded/running BEAM applications use functions from elixir Application module (e.g. Application.all_started/0)
  • for working with loaded/complied module use erlang :code, e.g. `:code.all_loaded/0’
  • to fetch all functions/macros/callbacks of a given module use builtin functions __info__/1, module_info/0,1, `behaviour_info/1
  • Protocols provide some info by means of __protocol__/1, impl_for/1, impl_for!/1 functions
  • Struct can be converted to map with Map.from_struct/1 - use that to inspect/iterate over fields
  • Map can be converted to struct by putting special key e.g. Map.put(my_map, :__struct__, MyModule)
  • It is possible to generate code and compile modules on the fly (look into Code)
  • module alias is just an atom, you can generate it in the runtime by format string expressions, e.g. :"Elixir.My.#{prefix}Module"
  • you can invoke any function with apply
  • use Code.fetch_docs to query documentation
  • use Code.Typespec.fetch_specs/callbacks/types to query dialyzer specs (warning - private API)
  • use macros and Macro.pre/postwalk to generate/transform AST
10
Post #8
gregvaughn

gregvaughn

The closest thing I think of to Java reflection is the __info__/1 (https://hexdocs.pm/elixir/1.8.1/Module.html#c:info/1) function defined on each module. It lets you find out what function/arity are exported by each module, plus other metadata.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Reflection in Java is generally focused on classes, and since Elixir does not have classes or objects, there is nothing particularly equivalent to Java reflection at all.

Keep in mind, reflection is a tool that exists to solve problems a particular way. Elixir is an entirely different approach to programming than Java, and so it has entirely different tools. The best way to learn Elixir is on its own terms, and not try too hard to find Java equivalents.

shanesveller

shanesveller

There aren’t really any successfully developed or adopted solutions for runtime reflection/inspection of Elixir modules, and in most cases we would favor compile-time metaprogramming. This might be done with macros or with just simple uses of quote/unquote. Chris McCord’s book is excellent here and does not suffer from its age as these facilities haven’t changed much over time since it was published.

shanesveller

shanesveller

This particular piece works fine as-is, but is safest to do with Module.concat/1 or better still, Module.safe_concat/1 or their /2 variants.

Where Next?

Popular in Questions Top

Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement