What is MFA in Elixir?

I’m posting this to force myself to remember what it means.

M : Module

F : Function

A : Arguments

Module, Function, Arguments.

pid = spawn(MyModule, :myfunc, [])

E.g.

iex(1)> pid = spawn(Explorate, :loop, [])
#PID<0.123.0>

Can then send that pid a message.

defmodule Explorate do

  def loop do
    receive do
      { :hello } -> IO.puts "ahoy, shipmate!"
      _ -> IO.puts "Yous no ma shipmayte"
    end

    loop
  end

end
Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> pid = spawn(Explorate, :loop, [])
#PID<0.123.0>
iex(2)> send(pid, "ddd")
Yous no ma shipmayte
"ddd"
iex(3)> send(pid, {:hello})
ahoy, shipmate!
{:hello}

Useful link

Note also to call loop  again to recurse, otherwise you only get to call the pid  once.

Published by

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.