altuntasfatih

altuntasfatih

Is pattern matching posible in custom guard expressions?

Hey guys,
I have a question about custom guard expression.I am trying to write multiple guard expressions with pattern matching however it doesn’t work . not sure it is possible :slight_smile:
Briefly I wan to use guards like functions
this is a case which ı wan to write

defguard is_position({x,y}) when is_integer(x) and is_integer(y)
defguard is_position(_) when false

defp create_robot(_,  p) when not is_position(p) ,do: {:error, "invalid position"}

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @altunasfatih it is not possible to do pattern matching in guards. However you can achieve what you want by combining guards in this specific case:

defguard is_position(term) when tuple_size(term) == 2
  and is_integer(elem(term, 0))
  and is_integer(elem(term, 1))
jwarlander

jwarlander

Another way to do this would of course be to use structs…

defmodule Position do
  defstruct [:x, :y]

  def new(x, y) when is_integer(x) and is_integer(y) do
    %Position{x: x, y: y}
  end
end

defp create_robot(%Position{x: x, y: y}) do
  #... 
end

It’s a matter of preference, and how well it fits the use case of course. In this scenario you’re trusting those who work with the code to always use Position.new/2 to create the struct, if you want to be absolutely sure it always contains integers.

You could also refine the code so that the struct enforces the keys to be defined, and has a typespec; then you will get nice documentation of the expectations, and make it possible to check the constraints using Dialyzer:

defmodule Position do
  @enforce_keys [:x, :y]
  defstruct [:x, :y]
  @type t :: %__MODULE__{x: integer(), y: integer()}

  @spec new(integer(), integer()) :: Position.t()
  def new(x, y) when is_integer(x) and is_integer(y) do
    %Position{x: x, y: y}
  end
end

defp create_robot(%Position{x: x, y: y}) do
  #... 
end

Where Next?

Popular in Questions Top

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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement