BrightEyesDavid

BrightEyesDavid

Double-exclamation mark syntax

Hi all. I was searching for ways to determine whether or not an Ecto query results in (a) result(s) without requiring the database to select columns as I don’t need the data and found this discussion in the Slack archive.

@michalmuskala shared this function which one can put in an app’s Repo module:

def exists?(query, opts \\ []) do
  query = from q in query, select: true, limit: 1
  case one(query, opts) do
    nil -> false
    true -> true
  end
end

An alternative to the case, which Paul Smith shared, which also gives true or false is:

!!one(query, opts)

I haven’t been able to find info on where the !! comes from and exactly what it does, so I thought I’d ask here. Is it a type coercion type of thing, and does anyone know where it’s documented? Apologies if this is just me utterly failing at using docs.

Thanks.

Marked As Solved

kokolegorille

kokolegorille

It enforces boolean value…

! is not

!! is not not, forcing true or false

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Right. !! isn’t a separate thing any more than not not true is. It’s just double application of a unary operator.

BrightEyesDavid

BrightEyesDavid

Thanks. I’ve just tested !!!, and that seems to be not-not-not, so I suppose the reason that !! isn’t explicitly documented (unless I’ve just missed it) is that it’s simply applying additional negation. (By calling a function twice?)

Last Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Right. !! isn’t a separate thing any more than not not true is. It’s just double application of a unary operator.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
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

We're in Beta

About us Mission Statement