简体   繁体   中英

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 ... ?

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM