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?)

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New

Other popular topics Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement