akshayde85

akshayde85

Hello All,

While comparing NIL vs true/false value, why elixir gives different results when one performs NIL and false operation? Please see below o/p from iex terminal ..

iex>> nil && true
nil
iex>> true && nil
nil
iex>> nil && false
nil
iex>> false && nil
false

In above, the last output seems incorrect logically. Is this a bug or intentional while designing ?

Showing Posts 1 to 7

Sebb

Sebb

&& will stop evaluating expressions if the first is falsy (and return the first)

Nicd

Nicd

As explained to you on Discord, this is a deliberate design. The documentation states:

evaluates and returns the second expression only if the first one evaluates to a truthy value (neither false nor nil). Returns the first expression otherwise.

In your last example, it is returning the first expression (false) as it did not evaluate to a truthy value.

heathen

heathen

It has no any sense to fully evaluate logical and in case the first operand is false and nil is a false equivalent. So if the first operand in a logical and is nil - it will always be returned.

It is the same as

iex(1) > exp = nil
iex(2) > result = (exp && "never be there") || "will be there"
"will be there"
Nicd

Nicd

Before spending time on this question, people may take use of this additional context from the Discord server:

Earlier chat on Discord server

akshayde85 Today at 09:50

Hello All, while doing && operation, why false and NIL gives different results? Please see the last statement of below iex

iex>> nil && true nil iex>> true && nil nil iex>> nil && false nil iex>> false && nil false

Rationally the last check of > false && nil should return nil, but its not, is this a bug or valid scenario?

dodo Today at 10:00

That’s correct, it is short-circuiting, so it already returns at false and doesn’t check the second condition. See Kernel — Elixir v1.20.2.

Nicd Today at 10:00

evaluates and returns the second expression only if the first one evaluates to a truthy value (neither false nor nil). Returns the first expression otherwise.

akshayde85 Today at 10:04

ok, as far as my knowledge of data goes( mostly financial systems), while doing &&/and operation, NIL is the supreme truth, for example lets take example as 1. Customer just opened account so balance will be NIL 2. KYC of customer done? > false … so if you circuit both, the answer should be NIL rather than false. I have changed the condition of code to handle above scenario, but somehow i find this logically incorrect

same goes with ||/or operation, true is supreme truth

Nicd Today at 10:06

these deal with truthiness values. in Elixir false and nil are falsy and everything else is truthy

if you want just booleans, use and/or, they only work with boolean values (i.e. not nil)

I don’t know what you mean with “supreme truth” though

akshayde85 Today at 10:25

Alternative example supreme truth will be of maths, just like you cannot divide by 0 or do operations like 0 * infinity because NULL/NIL/divide by 0 are referred as “I dont know” types values; true and false or 1 or 0 are values which we can refer to and perform many checks on them; so when you combine NIL “and/&&” true/false, the outcome always should be NIL

irrespective of order of NIL false true

so if you ask me … is dodo above 7ft tall ? Answer will be - I dont know / NIL / NULL ; is Nicd drives Tesla ? Answer will be I dont know / NIL/ NULL .. so if i take both of above q’s and answers, I cannot simply compare NULL of one answer with NULL or another answer so and/&& operation will be NULL/NIL but if any one of them or both of them are true/false types, I can definitely circuit them and can expect 1/0 true/false

I hope above will be clear any doubts

Nicd Today at 10:31

ah I guess you are thinking in terms of how SQL handles NULL values

Nicd Today at 10:32

@akshayde85 this is not how most programming values handle null/nil. they treat it as falsy, i.e. it will be evaluated the same way as false in a condition. this is how && also works

akshayde85 Today at 10:34

@Nicd .. thanks but somehow i feel thats incorrect, anyway i have handled the checks, reason i asked was i am taking values from table and doing && and it was giving different results

Nicd Today at 10:34

again, this is how most (high level) programming languages handle it

akshayde85 — Today at 10:50

= operator NULL true false
NULL NULL NULL NULL
true NULL true false
false NULL false true

and op NULL true false
NULL NULL NULL false
true NULL true false
false false false false

or op NULL true false
NULL NULL true NULL
true true true true
false NULL true false
if you see above AnSI SQL standards, false and null and NULL and false are > false
not other way round like one is false but another one is NULL/NIL

Nicd — Today at 10:51

but Elixir is not SQL

akshayde85 — Today at 10:52

I agree, they are not same, but these are basic CS standards any system should follow, btw, I am not blaming elixir here :slight_smile:

Nicd — Today at 10:55

I mean, SQL is pretty unique in how it handles null. I can’t name any other language that does it like that. so I wouldn’t call it a “basic CS standard”
so, anyway this is how Elixir works:

evaluates and returns the second expression only if the first one evaluates to a truthy value (neither false nor nil). Returns the first expression otherwise.

akshayde85

akshayde85 OP

Thanks all for the reply, in my case I was doing operation like below >

expr_1 = select operation from table A

expr_2 = select xyz column from table B where …

Then finally expr_3 = expr_1 && expr_2

Here, while doing some design changes, I accidentally ended up deleting data from table A for expr_1 ( which was there earlier and was returning data ), so whole logic in rest of code just failed because it evaluated it as “NIL && false” which returned NIL. I have added some checks now to correct this.

akshayde85

akshayde85 OP

Not sure how many of you had or have faced nightmares because of handling of NULL in different databases or
tools. NULL is very common nightmare ( NULLs are valid though) because generally people dont anticipate them
and single NULL can cause tremendous pain while summing or doing validations. I have used Oracle, Teradata,
Sybase, Hana, MySQL, Mongo, Abinitio and Informatica, JCL, COBOL in past and faced many issues because of it :slight_smile:

pavancse17

pavancse17

Ahh!! You can easily fix this by doing !!exp_1 && !!expr_2. It will always give your expected output either true or false.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews