Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of "!", "?", "_", and "." syntax in elixir

Tags:

elixir

I need help regarding understanding the following syntaxes in elixir !, ?, _, and .. What's those syntaxes role in elixir's function? For example Repo.get!.

I'm not sure whether they were just function name, or has a role. Though I know . is for calling anonymous function. And _ for any or variadic?

like image 236
ardhitama Avatar asked Sep 08 '15 04:09

ardhitama


People also ask

What is underscore in Elixir?

We use it to match any variable and discard it, because Elixir considers it permanently unbound: iex(38)> _ ** (CompileError) iex:38: unbound variable _ But when I assign something to underscore, the value gets echoed the same way it does in the case of normal variable binding: iex(38)> x = 10 10 iex(39)> _ = 10 10.

What does the exclamation mark mean in Elixir?

A trailing bang (exclamation mark) signifies a function or macro where failure cases raise an exception.

How do you check data type in Elixir?

Starting in elixir 1.2 there is an i command in iex that will list the type and more of any Elixir variable. If you look in the code for the i command you'll see that this is implemented via a Protocol.

How do you write functions in Elixir?

We can define functions with names so we can easily refer to them later. Named functions are defined within a module using the def keyword. Named functions are always defined in a module. To call named functions, we need to reference them using their module name.


2 Answers

! - Convention for functions which raise exceptions on failure.

? - Convention for functions which return a boolean value

_ - Used to ignore an argument or part of a pattern match expression.

. - As you mentioned is used for calling an anonymous function, but is also used for accessing a module function such as Mod.a(arg).

like image 151
bitwalker Avatar answered Oct 02 '22 00:10

bitwalker


Firstly ! and ?

They are naming conventions usually applied to the end of function name and are not any special syntax.

! - Will raise an exception if the function encounters an error.

One good example is Enum.fetch!(It also has a same Enum.fetch which does not raise exception).Finds the element at the given index (zero-based). Raises OutOfBoundsError if the given position is outside the range of the collection.

? - Used to show that the function will return a boolean value, either true or false. One good example is Enum.any? that returns true if functions is true for any value, otherwise return false

_ - This will ignore an argument in function or in pattern matching. If you like you can give a name after underscore.Ex - _base

This is commonly used in the end of a tail recursive function. One good example is the power function. If you want to raise any number base to 0 the result it 1, so it really does not matter what base is

defp getPower(_base,0), do: 1

. - Used to access any function inside the module or as you suggested calling an anonymous function

iex(1)> square = fn(number) -> number * number end
iex(2)> square.(4)
like image 26
coderVishal Avatar answered Oct 02 '22 00:10

coderVishal