Elixir Programming Language – Basic Rules and Syntax

0
125
Elixir programming language

Elixir is a vibrant and well-designed language for developing scalable and maintainable software applications. It is well known to leverage the Erlang VM which runs a low-latency, distributed and fault-tolerant systems. It is widely used for building web-based applications and the embedded software-based systems. Elixir Programming language has the following features.

    • Scalability − Elixir based applications are executed inside lightweight processes which are isolated and where the information is exchanged through messages.
    • Fault Tolerance − Elixir has one of the best fault tolerant systems where supervisors in Elixir help to describe how to take actions in case of failure to make sure that the recovery from failure is guaranteed.
    • Functional Programming – It is a well-designed programming language that promotes a coding style to enable developers to write short, fast, and maintainable code.
    • Build tools − Elixir supplies a set of development tools in its package. E.g. Mix is one of such tool that helps in the projects creation, tasks management, tests execution, etc. Hex, a project management tool is another example.
    • Erlang Compatibility − Elixir code executes on the Erlang VM which provides developers the complete access to Erlang’s ecosystem.

Environment Setup

Elixir based program executes on Erlang VM. Therefore, it is required to have Erlang on your local machine. Some platform supplies Elixir package along with Erlang. Given below is the link through which Elixir can be downloaded.

Elixir Download

https://elixir-lang.org/install.html

Installing Elixir on Windows

Elixir is available for different Operating Systems such as Windows, Linux, Mac, etc. In this article, we are going to use below link in order to download installer for Elixir installation on Windows operating system.

Elixir Download

https://elixir-lang.org/install.html#windows

STEP 1: After installer has downloaded successfully, click on the exe file. It will open the dialogue box shown below. Click on Next button.

setup elixir

STEP 2: It will display various Elixir versions. We need to choose one version out of the displayed list. We are going to choose the latest version i.e. Elixir version 1.6.5 and click on Next button.

Select elixir release

STEP 3: Next, we need to select Additional task to install Erlang. As discussed earlier that Erlang VM must to present on the local machine in order execute Elixir code. This step will install Erlang OTP 20.2 (64-bit). Choose this option and click on the Next button.

select additional tasks

STEP 4: Installer will take couple of minutes to download additional files and complete installation of Elixir and Erlang on the local machine.

download additional files

STEP 5: Next, click on the Next button after choosing the components as shown below.

choose elixir programming components

STEP 6: Next, choose the local installation path in order to setup and install Erlang OTP 20 as shown below. Click next to proceed with the installation.

choose installation location

STEP 7: Click on the close button to complete the installation setup as shown below.

Complete the installation of elixir programming

STEP 8: Now, lets test if Elixir setup has successfully installed on our system. We need to open a terminal on windows and enter iex on it. If the installation was successful, then it will open up the interactive elixir shell as shown below.

interactive elixir shell

Elixir Programming Language – Basic Syntax and rules

In this article, we are going to use the Elixir script mode where we can save the Elixir code into a file with the extension “. ex”.

  • A basic example to print string

STEP 1: Let’s us keep the simple code to print string “Hello Elixir” into the “test_elixir. ex” file as shown below.

test_elixir. ex

IO.puts “Hello Elixir”

STEP 2: Now, we will execute the code present in above file in the terminal as shown below. It will print the string.

print string

Comments: When the developer wants to enter texts in the program and don’t want that part of code to compile and execute such lines of code are known as comments. Single line comments in Elixir programming language start with a ‘#’ symbol. It does not support multi-line comment, but we can stack multiple comments. For example,

Comments

#This is how we provide comment in Elixir

  • Line Endings: We can place multiple statements in the same line by simply using ‘;’. In Elixir, there are no required line endings like ‘;’. For example,

Comments

IO.puts (“Hello”); IO.puts(“Elixir!”)

comand prompt

  • Identifiers: In Elixir, identifiers such as variables, function names are used to identify a variable, function, etc. respectively. We can name our identifiers starting with a lower case alphabet, followed by numbers, underscores and upper case letters subsequently. It has naming convention which is commonly known as snake_case. The following are some of the valid identifiers examples used in Elixir.

Identifiers

variable1

var_2

one_N0r3_var

This should be noted that in Elixir, we can also name variables with a leading underscore. Any value which is not meant to be used must be assigned to _ or to a variable which starts with underscore. Example is shown below.

Identifiers

_any_random_val = 42

Also, elixir programming language relies on underscores in order to make functions private to the modules. If we name any function with a leading underscore in a module and import that module, then this function will not be imported since it has made private with the use of leading underscore.

  • Reserved Words: The following are the reserved words in Elixir which cannot be used as variables, module or function names.

Reserved Words

after

and

catch

do

inbits

inlist

nil

else

end

not

or

false

fn

in

rescue

true

when

xor

__MODULE__

__FILE__

__DIR__

__ENV__

__CALLER__

Conclusion

In this article, we discussed about Elixir programming language and its dependency on Erlang VM. We have demonstrated various steps to install Elixir and Erlang on windows machine and further discussed on Elixir basic syntax and rules along with suitable examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here