-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Rework if/2 docs and mention variable scope #14452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: Parker Selbert <[email protected]>
lib/elixir/lib/kernel.ex
Outdated
@@ -3898,51 +3898,63 @@ defmodule Kernel do | |||
|
|||
## One-liner examples | |||
|
|||
if(foo, do: bar) | |||
iex> if 7 > 5, do: "yes" | |||
"yes" | |||
|
|||
In the example above, `bar` will be returned if `foo` evaluates to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the example above, "yes"
is returned because 7 > 5
evaluates to a truthy value (true
in this case). nil
will be returned if the condition evaluates to a falsy(nil
or false
) value:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dmitrykleymenov, good catch!
Fixed in 96bbccf
I'm just curious if it should be mentioned that the code inside unmatched blocks isn't even evaluated, and it's not only about a return value. |
It's true that it wasn't explicitly called out, added an example in 6d56287 |
Co-authored-by: José Valim <[email protected]>
The structure of the
if
doc was a bit hard to understand, since:nil
will bereturned.")
if(foo, do: bar)
andif foo, do: bar
form, but AFAIK the former is less common and idiomatic nowadays?I also noticed that the fact we can define a variable in the
if
condition wasn't explicitly documented, as well as scope rules more broadly (even if they aren't specific toif
, it's probably the more common place people encounter it). Inspired from this