What is the purpose of ? (question mark) in calling a function?

Asked

Viewed 165 times

7

I’m studying the module Enum of the Elixir language in which it provides various functions that allows working with enumerables, However, one thing caught my attention in the call of some of the functions.

The function any when calling she is required to inform a ? otherwise of the error, see the examples:

array = [1, 23, 54, 1, 121]
r = Enum.any?(array,
    fn(v) ->
        v > 100
    end
)
IO.puts(r)

Exit:

true

Without the ? in function call:

r = Enum.any(array,
    fn(v) ->
        v > 100
    end
)

Exit:

** (UndefinedFunctionError) function Enum.any/2 is undefined or private. Did you mean one of:

      * any?/1
      * any?/2

    (elixir) Enum.any([1, 23, 54, 1, 121], #Function<1.115443437 in file:main.exs>)
    main.exs:26: (file)
    (elixir) lib/code.ex:363: Code.require_file/2

Doubts

  1. What is the purpose of ? in a function?
  2. What is the relationship of ? with the signature of the function?
  3. What the ? plays a special role?

2 answers

6


What is the purpose of ? in a function?

Coding style and naming convention only.

What is the relationship of ? with the signature of the function?

No real, just a conventional.

What the ? plays a special role?

In language, nothing.

But it is agreed that functions that result in a Boolean value must have their name post-fixed with this character to indicate that it is an inquiry that must be answered with a yes or no. You may just not use, or use differently, but you will be running away from the recommendation. It’s just a character like any other in the name.

The error is only because you have not used the full name, if you have a function returning boolean without the ? in the name has to call without the ?, as if a function EVerdade is different from EhVerdade.

Documentation.

  • The interesting thing is that in other languages the ? at the end of the syntax name invalidates.

  • Yes, for the most part.

3

The Elixir documentation itself talks about this in "Naming Conventions".

Interrogation?

The question mark in the Elixir indicates that the function returns a boolean. Note that not only in functions, but also in variables.

iex> active? = (1 + 1 == 2)
true

Note that the Elixir guard clauses do not have the same convention, but follow the style of Erlang, with is_*, instead of *?.

iex> defmodule Math do
iex>   def double(arg) when is_number(arg) do
iex>     ...
iex>   end
iex>
iex>   def double(arg) when is_list(arg) do
iex>     ...
iex>   end
iex> end

The guard clasps above are defined with is_number and is_list, and without question.

Exclamation!

The exclamation mark in Elixir indicates that the function can generate an exception, rather than the error tuple, which is standard in Elixir. See:

File.read("file.txt")
{:ok, "file contents"}
iex> File.read("no_such_file.txt")
{:error, :enoent}

iex> File.read!("file.txt")
"file contents"
iex> File.read!("no_such_file.txt")
** (File.Error) could not read file no_such_file.txt: no such file or directory

Browser other questions tagged

You are not signed in. Login or sign up in order to post.