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.

peerreynders

peerreynders

  • Is there strict order rules, e.g. can it also be rules = %Rules{…} instead ?

IIRC that order will work as well.

Why doesn’t it complain about the other two keys that are defined within the module’s struct definition?

Complain about what? It’s a pattern - not a value. The pattern doesn’t care about the player1: and player2: keys and their values.

AlchemistCamp

AlchemistCamp

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

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement