cjbottaro

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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. :person_shrugging:

cjbottaro

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… :slight_smile:

Last Post!

dimitarvp

dimitarvp

I don’t object to anything you said, I am just wondering what kind of code you’re looking to write in the end, hence me earlier reply.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement