Search code examples
erlangelixirejabberd

How can I translate this piece of code from Erlang to Elixir?


How can I translate this piece of code from Erlang to Elixir?

Erlang:

xmpp:get_text(Packet#message.body)

Elixir:

:xmpp.get_text(packet ... ?

Solution

  • This is the record field access syntax. That code accesses the field body of the term Packet assuming that the Packet is a message record. Elixir provides a Record module to deal with this. You need to first import the record's definition using Record.extract (change the .hrl path to point to the dependency's hrl file that includes the definition of the record; search for -record(message in the library you're using) and then use that definition. Here's an example:

    defmodule A do
      Record.defrecord :message, Record.extract(:message, from_lib: "path/to/file.hrl")
    end
    

    Now Packet#message.body translates to A.message(packet, :body).