Vertigo

Vertigo

Could anyone explain this function signature

Reading/practicing through “Functional Web Development with Elixir, OTP and Phoenix”, I came across the following module definition;

defmodule IslandsEngine.Rules do
  alias __MODULE__

  defstruct state: :initialized,
            player1: :islands_not_set,
            player2: :islands_not_set

  def new(), do:
    %Rules{}

  def check(%Rules{state: :initialized} = rules, :add_player), do:
    {:ok, %Rules{rules | state: :players_set}}

  ...
end

What is the reason for the first agument’s right hand side declaration (‘= rules’) is it simply a definition of the working variable within scope ?

def check(%Rules{state: :initialized} = rules, :add_player), do:
{:ok, %Rules{rules | state: :players_set}}

Running iex -S mix I see no practical difference passing in a different ‘variable’ name or direct struct, but perhaps I am missing something obvious;

Interactive Elixir (1.7.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> alias IslandsEngine.Rules
IslandsEngine.Rules
iex(2)> things = Rules.new()
%IslandsEngine.Rules{
  player1: :islands_not_set,
  player2: :islands_not_set,
  state: :initialized
}
iex(3)> Rules.check(things, :add_player)
{:ok,
 %IslandsEngine.Rules{
   player1: :islands_not_set,
   player2: :islands_not_set,
   state: :players_set
 }}
iex(4)> Rules.check(%IslandsEngine.Rules{state: :initialized}, :add_player)
{:ok,
 %IslandsEngine.Rules{
   player1: :islands_not_set,
   player2: :islands_not_set,
   state: :players_set
 }}

Marked As Solved

peerreynders

peerreynders

What is the reason for the first agument’s right hand side declaration (’= rules’) ?

rules is the same as in:

def check(rules, :add_player), do:

the difference is that

def check(%Rules{state: :initialized} = rules, :add_player), do:

adds the constraint of a pattern match.

Also Liked

NobbZ

NobbZ

Anything that matches the pattern %Rules{state: :initialized} will be bound to rules in the function body.

Vertigo

Vertigo

Thanks for replying!

I reasoned that meanwhile.., two related questions;

  • Is there strict order rules, e.g. can it also be rules = %Rules{…} instead ?
  • Why doesn’t it complain about the other two keys that are defined within the module’s struct definition?
peerreynders

peerreynders

Pattern matches on maps aren’t exact. The map can contain information in addition to the data that is matched.

Last Post!

AlchemistCamp

AlchemistCamp

Yep, both orders work. Great book you’re going through, BTW!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New

We're in Beta

About us Mission Statement