cjbottaro
How let a function be used in a guard like is_binary/1
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!
Most Liked
benwilson512
You can’t, guards are fundamentally defined in the VM itself, which allows the VM to perform specific optimizations. defguard lets you essentially construct “new” guards by composing together other guards, but you can’t make new “from scratch” guards.
dimitarvp
I don’t see the value of guards over this:
def do_stuff(%Opportunity{type: "Opportunity::Automatch"}), do: ...
def do_stuff(%Category{type: "Category::Automatch"}), do: ...
…and nothing else. Elixir will give you a runtime error if you pass anything else because there’s no clause to handle it. ![]()
cjbottaro
I think I worded my question poorly, but anyways, the docs clearly explain how to do what I want…
(defguard) creates a macro that can be used both inside or outside guards.
defmodule Foo do
defguard bar(m) when is_map(m) and m.field == true
end
iex> require Foo
iex> Foo.bar(%{field: true})
true
I just needed to read more better… ![]()







