Kernel (Elixir v1.11.4) View Source

Kernel is Elixir's default environment.

It mainly consists of:

    basic language primitives, such as arithmetic operators, spawning of processes, data type handling, and others
    macros for control-flow and defining new functionality (modules, functions, and the like)
    guard checks for augmenting pattern matching

You can invoke Kernel functions and macros anywhere in Elixir code without the use of the Kernel. prefix since they have all been automatically imported. For example, in IEx, you can call:

iex> is_number(13)
true

If you don't want to import a function or macro from Kernel, use the :except option and then list the function/macro by arity:

import Kernel, except: [if: 2, unless: 2]

See Kernel.SpecialForms.import/2 for more information on importing.

Elixir also has special forms that are always imported and cannot be skipped. These are described in Kernel.SpecialForms.
The standard library

Kernel provides the basic capabilities the Elixir standard library is built on top of. It is recommended to explore the standard library for advanced functionality. Here are the main groups of modules in the standard library (this list is not a complete reference, see the documentation sidebar for all entries).
Built-in types

The following modules handle Elixir built-in data types:

    Atom - literal constants with a name (true, false, and nil are atoms)
    Float - numbers with floating point precision
    Function - a reference to code chunk, created with the fn/1 special form
    Integer - whole numbers (not fractions)
    List - collections of a variable number of elements (linked lists)
    Map - collections of key-value pairs
    Process - light-weight threads of execution
    Port - mechanisms to interact with the external world
    Tuple - collections of a fixed number of elements

There are two data types without an accompanying module:

    Bitstring - a sequence of bits, created with Kernel.SpecialForms.<<>>/1. When the number of bits is divisible by 8, they are called binaries and can be manipulated with Erlang's :binary module
    Reference - a unique value in the runtime system, created with make_ref/0

Data types

Elixir also provides other data types that are built on top of the types listed above. Some of them are:

    Date - year-month-day structs in a given calendar
    DateTime - date and time with time zone in a given calendar
    Exception - data raised from errors and unexpected scenarios
    MapSet - unordered collections of unique elements
    NaiveDateTime - date and time without time zone in a given calendar
    Keyword - lists of two-element tuples, often representing optional values
    Range - inclusive ranges between two integers
    Regex - regular expressions
    String - UTF-8 encoded binaries representing characters
    Time - hour:minute:second structs in a given calendar
    URI - representation of URIs that identify resources
    Version - representation of versions and requirements

System modules

Modules that interface with the underlying system, such as:

    IO - handles input and output
    File - interacts with the underlying file system
    Path - manipulates file system paths
    System - reads and writes system information

Protocols

Protocols add polymorphic dispatch to Elixir. They are contracts implementable by data types. See Protocol for more information on protocols. Elixir provides the following protocols in the standard library:

    Collectable - collects data into a data type
    Enumerable - handles collections in Elixir. The Enum module provides eager functions for working with collections, the Stream module provides lazy functions
    Inspect - converts data types into their programming language representation
    List.Chars - converts data types to their outside world representation as charlists (non-programming based)
    String.Chars - converts data types to their outside world representation as strings (non-programming based)

Process-based and application-centric functionality

The following modules build on top of processes to provide concurrency, fault-tolerance, and more.

    Agent - a process that encapsulates mutable state
    Application - functions for starting, stopping and configuring applications
    GenServer - a generic client-server API
    Registry - a key-value process-based storage
    Supervisor - a process that is responsible for starting, supervising and shutting down other processes
    Task - a process that performs computations
    Task.Supervisor - a supervisor for managing tasks exclusively

Supporting documents

Elixir documentation also includes supporting documents under the "Pages" section. Those are:

    Compatibility and Deprecations - lists compatibility between every Elixir version and Erlang/OTP, release schema; lists all deprecated functions, when they were deprecated and alternatives
    Library Guidelines - general guidelines, anti-patterns, and rules for those writing libraries
    Naming Conventions - naming conventions for Elixir code
    Operators - lists all Elixir operators and their precedences
    Patterns and Guards - an introduction to patterns, guards, and extensions
    Syntax Reference - the language syntax reference
    Typespecs- types and function specifications, including list of types
    Unicode Syntax - outlines Elixir support for Unicode
    Writing Documentation - guidelines for writing documentation in Elixir

Guards

This module includes the built-in guards used by Elixir developers. They are a predefined set of functions and macros that augment pattern matching, typically invoked after the when operator. For example:

def drive(%User{age: age}) when age >= 16 do
  ...
end

The clause above will only be invoked if the user's age is more than or equal to 16. Guards also support joining multiple conditions with and and or. The whole guard is true if all guard expressions will evaluate to true. A more complete introduction to guards is available in the "Patterns and Guards" page.