# \#guard-clauses

**URL:** https://forum.elixirforum.com/tag/guard-clauses/131.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [My guard definition is coming up cannot invoke remote function](https://forum.elixirforum.com/t/my-guard-definition-is-coming-up-cannot-invoke-remote-function/73892)

<div class="topic-metadata">

**Author:** [@roganjoshua](https://forum.elixirforum.com/u/roganjoshua)\
**Replies:** 3\
**Last updated:** [January 6, 2026, 4:51pm UTC](https://forum.elixirforum.com/t/my-guard-definition-is-coming-up-cannot-invoke-remote-function/73892 "2026-01-06T16:51:27Z")

</div>

I have defined a guard in a definitions module and file. defguard is\_maximum\_tiles\_in\_rack(value) when is\_integer(value) and value \> @max\_num\_tiles\_in\_rack I have ali…

---

## [Writing a keyword list guard?](https://forum.elixirforum.com/t/writing-a-keyword-list-guard/58347)

<div class="topic-metadata">

**Author:** [@rcm765](https://forum.elixirforum.com/u/rcm765)\
**Replies:** 13\
**Last updated:** [July 11, 2025, 7:24am UTC](https://forum.elixirforum.com/t/writing-a-keyword-list-guard/58347 "2025-07-11T07:24:47Z")

</div>

I’d like a guard to determine if the value is a keyword list or not. I’ve tried the following macro: defmacro is\_keyword(\[{key, \_value} | \_tail\]), do: quote do: is\_atom(unquote(key)) but when I pass in a keyword list I…

---

## [CompileError: "invalid use of \_. "\_" represents a value to be ignored in a pattern and cannot be used in expressions"](https://forum.elixirforum.com/t/compileerror-invalid-use-of-represents-a-value-to-be-ignored-in-a-pattern-and-cannot-be-used-in-expressions/44818)

<div class="topic-metadata">

**Author:** [@Maxximiliann](https://forum.elixirforum.com/u/Maxximiliann)\
**Replies:** 10\
**Last updated:** [January 30, 2023, 3:03pm UTC](https://forum.elixirforum.com/t/compileerror-invalid-use-of-represents-a-value-to-be-ignored-in-a-pattern-and-cannot-be-used-in-expressions/44818 "2023-01-30T15:03:30Z")

</div>

This case statement is perfectly functional, but it repeats code: case foo do {:pass, \_} = reason -\> Logger.info(reason) {:ok, \_, \_} -\> status = Keyword.get(opts, :status) some\_func(status) Logger.…

---

## [How let a function be used in a guard like is\_binary/1](https://forum.elixirforum.com/t/how-let-a-function-be-used-in-a-guard-like-is-binary-1/52600)

<div class="topic-metadata">

**Author:** [@cjbottaro](https://forum.elixirforum.com/u/cjbottaro)\
**Replies:** 9\
**Last updated:** [December 18, 2022, 7:34pm UTC](https://forum.elixirforum.com/t/how-let-a-function-be-used-in-a-guard-like-is-binary-1/52600 "2022-12-18T19:34:47Z")

</div>

I noticed that some function like is\_binary/1 is marked as “Allowed in guard tests”. How can I make my own functions/guards like that? If I defguard and def the same name, the compiler complains. Thanks!

---

## [Why can or be used in guard clauses but not ||?](https://forum.elixirforum.com/t/why-can-or-be-used-in-guard-clauses-but-not/52228)

<div class="topic-metadata">

**Author:** [@elt547](https://forum.elixirforum.com/u/elt547)\
**Replies:** 26\
**Last updated:** [December 5, 2022, 12:52pm UTC](https://forum.elixirforum.com/t/why-can-or-be-used-in-guard-clauses-but-not/52228 "2022-12-05T12:52:38Z")

</div>

Curious as to why this is not permitted. I suspect it has something to do with or only accepting a boolean as the first operand. But why does this matter with guard clauses?

---

## [What is considered 'bad' in a guard clause?](https://forum.elixirforum.com/t/what-is-considered-bad-in-a-guard-clause/52202)

<div class="topic-metadata">

**Author:** [@miguelszerman](https://forum.elixirforum.com/u/miguelszerman)\
**Replies:** 3\
**Last updated:** [December 2, 2022, 5:40pm UTC](https://forum.elixirforum.com/t/what-is-considered-bad-in-a-guard-clause/52202 "2022-12-02T17:40:13Z")

</div>

Inside the Elixir official documentation, the guards sections states: Not all expressions are allowed in guard clauses, but only a handful of them. This is a deliberate choice. This way, Elixir (and Erlang) can make su…

---

## [Can a defguard definition match against a struct?](https://forum.elixirforum.com/t/can-a-defguard-definition-match-against-a-struct/23761)

<div class="topic-metadata">

**Author:** [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Replies:** 14\
**Last updated:** [June 21, 2022, 8:33pm UTC](https://forum.elixirforum.com/t/can-a-defguard-definition-match-against-a-struct/23761 "2022-06-21T20:33:59Z")

</div>

Hello, Is it possible to check if an argument to a guard is a particular structure? Context: I am trying to have a defguard definition that checks if something is an Ecto query – which means it can either be a module (…

---

## [Strange exception handling by guard clause](https://forum.elixirforum.com/t/strange-exception-handling-by-guard-clause/44418)

<div class="topic-metadata">

**Author:** [@emeryotopalik](https://forum.elixirforum.com/u/emeryotopalik)\
**Replies:** 15\
**Last updated:** [December 13, 2021, 8:39pm UTC](https://forum.elixirforum.com/t/strange-exception-handling-by-guard-clause/44418 "2021-12-13T20:39:22Z")

</div>

Hello. I am experiencing an unexpected behavior with the way Elixir is handling a guard clause exception. Take the following: defmodule Foo do def eval(x, percentage) when rem(x, 100) \< percentage, do: true de…

---

## [It's possible to write a custom Guard to check for a blank string?](https://forum.elixirforum.com/t/its-possible-to-write-a-custom-guard-to-check-for-a-blank-string/31199)

<div class="topic-metadata">

**Author:** [@Exadra37](https://forum.elixirforum.com/u/Exadra37)\
**Replies:** 21\
**Last updated:** [March 10, 2021, 11:50pm UTC](https://forum.elixirforum.com/t/its-possible-to-write-a-custom-guard-to-check-for-a-blank-string/31199 "2021-03-10T23:50:07Z")

</div>

Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do\_stuff(string1, string2) when is\_binary(string1) and byte\_size(string1) \> 0 and…

---

## [Incompatible types warning on complex defguard within elixir 1.11.3](https://forum.elixirforum.com/t/incompatible-types-warning-on-complex-defguard-within-elixir-1-11-3/37885)

<div class="topic-metadata">

**Author:** [@xadhoom](https://forum.elixirforum.com/u/xadhoom)\
**Replies:** 4\
**Last updated:** [March 1, 2021, 9:53pm UTC](https://forum.elixirforum.com/t/incompatible-types-warning-on-complex-defguard-within-elixir-1-11-3/37885 "2021-03-01T21:53:30Z")

</div>

Well, while migrating to elixir 1.11.3 (from 1.10.4) I’ve a new warning about a somewhat complex guard expression that has incompatible types. An example code triggering that is something like: @allowed\_types \[ …

---

## [Please help me with example use case for WHEN in elixir](https://forum.elixirforum.com/t/please-help-me-with-example-use-case-for-when-in-elixir/36379)

<div class="topic-metadata">

**Author:** [@AungMyoOo168](https://forum.elixirforum.com/u/AungMyoOo168)\
**Replies:** 4\
**Last updated:** [December 22, 2020, 2:33pm UTC](https://forum.elixirforum.com/t/please-help-me-with-example-use-case-for-when-in-elixir/36379 "2020-12-22T14:33:31Z")

</div>

I want to know more about WHEN statement in elixir

---

## [Is it possible to write a guard clause that validates a regex?](https://forum.elixirforum.com/t/is-it-possible-to-write-a-guard-clause-that-validates-a-regex/34668)

<div class="topic-metadata">

**Author:** [@oliverandrich](https://forum.elixirforum.com/u/oliverandrich)\
**Replies:** 4\
**Last updated:** [September 30, 2020, 10:06pm UTC](https://forum.elixirforum.com/t/is-it-possible-to-write-a-guard-clause-that-validates-a-regex/34668 "2020-09-30T22:06:54Z")

</div>

Hi, I am currently trying to implement a guard which validates that a parameter is a valid URL. Is it possible to call URI.parse or Regex.match inside a guard clause? I am a newbie that is learning Elixir, and maybe I t…

---

## [How can I check if an argument is a valid IO list?](https://forum.elixirforum.com/t/how-can-i-check-if-an-argument-is-a-valid-io-list/28502)

<div class="topic-metadata">

**Author:** [@mmmries](https://forum.elixirforum.com/u/mmmries)\
**Replies:** 1\
**Last updated:** [January 23, 2020, 7:11am UTC](https://forum.elixirforum.com/t/how-can-i-check-if-an-argument-is-a-valid-io-list/28502 "2020-01-23T07:11:21Z")

</div>

I maintain a library where a user can publish a message and I want to accept any iodata (I end up writing it to a TCP connection), but I would to return an error as soon as possible if someone passes an argument that is …

---

## [Using macros in guard clauses](https://forum.elixirforum.com/t/using-macros-in-guard-clauses/26125)

<div class="topic-metadata">

**Author:** [@helicon](https://forum.elixirforum.com/u/helicon)\
**Replies:** 7\
**Last updated:** [October 17, 2019, 10:44am UTC](https://forum.elixirforum.com/t/using-macros-in-guard-clauses/26125 "2019-10-17T10:44:40Z")

</div>

Hey! I have a function @doc """ Check if given id has 64-bit size Examples: \> MyModule.has\_64\_bit\_size(132700114308300801) true \> MyModule.has\_64\_bit\_size(35) false """ def has\_64\_bit\_size(id) do bit\_size(:binary.…

---

## [Idiomatic: pattern matching with two function heads or boolean guard clause?](https://forum.elixirforum.com/t/idiomatic-pattern-matching-with-two-function-heads-or-boolean-guard-clause/20753)

<div class="topic-metadata">

**Author:** [@niels\_bom](https://forum.elixirforum.com/u/niels_bom)\
**Replies:** 4\
**Last updated:** [March 9, 2019, 7:19pm UTC](https://forum.elixirforum.com/t/idiomatic-pattern-matching-with-two-function-heads-or-boolean-guard-clause/20753 "2019-03-09T19:19:31Z")

</div>

I just found myself writing this: def foo(\[\], \_), do: \[\] def foo(\_, 0), do: \[\] But then I thought I could also use a guard clause with a boolean OR, like so: def foo(list, n) when list == \[\] or n == 0, do: \[\] Is eith…

---

## [Idea: 'multiguards' to allow structs in guards](https://forum.elixirforum.com/t/idea-multiguards-to-allow-structs-in-guards/18722)

<div class="topic-metadata">

**Author:** [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Replies:** 1\
**Last updated:** [December 13, 2018, 10:02pm UTC](https://forum.elixirforum.com/t/idea-multiguards-to-allow-structs-in-guards/18722 "2018-12-13T22:02:06Z")

</div>

Elixir allows you to define your own data-structures as structs, on top of Erlang maps. However, such user-defined types have always felt a little ‘off’ from the built-in types, because they do not get the same level of …

---

## [Idiomatic guard clause for checking not nil](https://forum.elixirforum.com/t/idiomatic-guard-clause-for-checking-not-nil/18296)

<div class="topic-metadata">

**Author:** [@gshaw](https://forum.elixirforum.com/u/gshaw)\
**Replies:** 3\
**Last updated:** [November 28, 2018, 8:03pm UTC](https://forum.elixirforum.com/t/idiomatic-guard-clause-for-checking-not-nil/18296 "2018-11-28T20:03:07Z")

</div>

What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt\_if\_not\_signed\_in(conn, signed\_in\_account) when signed\_in\_account != nil do Second way: defp halt\_if\_not\_signed\_in(conn, signed\_…

---

## [Function refs in guards](https://forum.elixirforum.com/t/function-refs-in-guards/16098)

<div class="topic-metadata">

**Author:** [@jgonet](https://forum.elixirforum.com/u/jgonet)\
**Replies:** 9\
**Last updated:** [August 26, 2018, 7:07pm UTC](https://forum.elixirforum.com/t/function-refs-in-guards/16098 "2018-08-26T19:07:16Z")

</div>

I was creating simple RPN calculator function, but I stumbled upon a problem in matching functions in guards. Code looks like so: def compute\_RPN\_expr(value, stack) when is\_number(value), do: \[value | stack\] def compu…

---

## [Can I check if value is in range in a guard clause](https://forum.elixirforum.com/t/can-i-check-if-value-is-in-range-in-a-guard-clause/14932)

<div class="topic-metadata">

**Author:** [@Scoty](https://forum.elixirforum.com/u/Scoty)\
**Replies:** 7\
**Last updated:** [July 2, 2018, 3:51pm UTC](https://forum.elixirforum.com/t/can-i-check-if-value-is-in-range-in-a-guard-clause/14932 "2018-07-02T15:51:20Z")

</div>

Hey, I am currently reading Programming Elixir and I am doing one of the exercises where you should write a solution to the “I’m thinking of a number between 1 and 1000” I have made it work, but I wanted to add a simpl…

---

## [Error defining a guard - invalid syntax in defguard](https://forum.elixirforum.com/t/error-defining-a-guard-invalid-syntax-in-defguard/13790)

<div class="topic-metadata">

**Author:** [@ijverig](https://forum.elixirforum.com/u/ijverig)\
**Replies:** 10\
**Last updated:** [April 23, 2018, 8:57pm UTC](https://forum.elixirforum.com/t/error-defining-a-guard-invalid-syntax-in-defguard/13790 "2018-04-23T20:57:55Z")

</div>

Using guards like this works fine: iex(1)\> defmodule M do ...(1)\> def update\_position({pos, max\_pos}) when pos \< max\_pos, do: pos + 1 ...(1)\> end {:module, M, … iex(2)\> M.update\_position({3,4}) 4 …

---

## [How to handle "bad argument in arithmetic expression" error by using guards](https://forum.elixirforum.com/t/how-to-handle-bad-argument-in-arithmetic-expression-error-by-using-guards/12954)

<div class="topic-metadata">

**Author:** [@acrolink](https://forum.elixirforum.com/u/acrolink)\
**Replies:** 3\
**Last updated:** [March 7, 2018, 9:59pm UTC](https://forum.elixirforum.com/t/how-to-handle-bad-argument-in-arithmetic-expression-error-by-using-guards/12954 "2018-03-07T21:59:25Z")

</div>

I want to learn some basics of this great langauge. I have put this code together: defmodule Xe do def call(input) do with {:ok, first} when is\_integer(input) \<- by\_ten(input), {:ok, second} \<- by\_five(f…

---

## [A possibility for extending the usability guard clauses](https://forum.elixirforum.com/t/a-possibility-for-extending-the-usability-guard-clauses/4512)

<div class="topic-metadata">

**Author:** [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Replies:** 1\
**Last updated:** [April 7, 2017, 6:23pm UTC](https://forum.elixirforum.com/t/a-possibility-for-extending-the-usability-guard-clauses/4512 "2017-04-07T18:23:47Z")

</div>

In Guard clauses (the when... part after the destructuring section of a pattern match), we’re only allowed to use a very small subset of functions because these are the only ones that the BEAM can optimize and that are k…

---

## [Design problem with three similar structs](https://forum.elixirforum.com/t/design-problem-with-three-similar-structs/1304)

<div class="topic-metadata">

**Author:** [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Replies:** 1\
**Last updated:** [August 6, 2016, 7:27pm UTC](https://forum.elixirforum.com/t/design-problem-with-three-similar-structs/1304 "2016-08-06T19:27:43Z")

</div>

I am working on the Tensor library. This adds three modules to your project: Vector, Matrix and Tensor. As you may or may not know, a Vector is just a one-dimensional Tensor, and a Matrix is a two-dimensional Tensor. T…
