惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
博客园 - Franky
U
Unit 42
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
B
Blog RSS Feed
雷峰网
雷峰网
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
美团技术团队
云风的 BLOG
云风的 BLOG
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
G
Google Developers Blog
T
Tailwind CSS Blog
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
月光博客
月光博客
Engineering at Meta
Engineering at Meta

Lobsters

CIFSwitch: a non-universal Linux local root vulnerability RIPE NCC session fixation: poaching logins with an Atlas probe GNOME 2.20 but its Web Components Agentic Search for Context Engineering – Leonie Monigatti Garnix is shutting down [not OC] akashina.tngl.sh/jjc Concerning Emacs (and Jazz) Nitpicking the shell history scene in ‘Tron: Legacy’ What's cooking on SourceHut? Q2 2026 The tenth OpenPGP email summit Package managers that package package managers Clojure on Fennel part three: parsing WordPress at 23 Finding Miscompiles for Fun, Not Profit GitHub - creusot-rs/creusot: Creusot helps you prove your Rust code is correct. Announcing Rust 1.96.0 | Rust Blog A Love Letter to Neovim sqlite AGENTS.md Am I a Bad Friend? CSS vs. JavaScript • Josh W. Comeau Erlang Ecosystem Foundation - Supporting the BEAM community A brief note about slot access cost in Common Lisp Keyboard latency probe Rethinking the GNOME clipboard issues Back to the Building Blocks’ Building Blocks Tech Notes: Theseus: translating win32 to wasm Fast is better than slow Content-addressed Rust builds (or, what kache actually caches) Intent to Prototype: Embedding API Canada’s Bill C-22 and the security cost of collecting more data
Hauleth's blog - Guards! Guards!
hauleth.dev · 2026-06-28 · via Lobsters

Let's start with simple quiz.


Given module defined as:

defmodule Foo do
  def a(x) when is_integer(x) or is_map_key(x, :foo), do: true
  def a(x), do: false

  def b(x) when is_map_key(x, :foo) or is_integer(x), do: true
  def b(x), do: false
end

Try to answer these questions.

Q: What will be result of Foo.a(%{foo: 21})?

true

false

WRONG

RIGHT

This one is straightforward.

We check guard, it has one condition is_integer(x) or is_map_key(x, :foo). First one returns false, second returns true, Boolean's alternative results in true and first case is matched.

Q: What will be result of Foo.a(37)?

true

false

WRONG

RIGHT

This one is straightforward as well.

We check guard, it has one condition is_integer(x) or is_map_key(x, :foo). First one returns true, second one isn't fired at all, because or operator is short circuiting.

Q: What will be result of Foo.b(%{foo: 21})?

true

false

WRONG

RIGHT

Again, similar to the previous questions.

We check guard, it has one condition is_map_key(x, :foo) or is_integer(x). First one returns true and the rest is short circuited.

Q: What will be result of Foo.b(37)?

true

false

WRONG

RIGHT

Ouch, something changed…

Again, we check guard, one condition is_map_key(x, :foo) or is_integer(x). We hit first clause is_map_key(x, :foo) and this doesn't return false, instead it fail. Failure in one of guard functions isn't converted to false but instead makes whole guard expression fail. This mean that is_integer(x) part will never be called.


This behaviour is often surprising for a lot of Elixir developers, as it seemingly breaks commutative property of boolean operators. However, to be honest, these never were commutative because of short circuiting.

It seems that Elixir at the time of writing (Elixir 1.20.1, OTP 29) do not warn about this issue.