Nicd

Nicd

Reading this topic, I came to the question:

What is the purpose of the Kernel module?

As I understand it, it somewhat mirrors the :erlang module and contains all functions available in guards. But the problem is, now some map functions are in both Map and Kernel, the same for List. So Kernel contains both functions that work on many datatypes and functions that work only on certain datatypes. If we add the new map_get/2 and is_map_key/2, then map functions will be even more split between Kernel and Map.

This is what I see in Kernel:

  1. Functions/macros necessary for the structure of the language or that are useful to be default imported, operators/def*/if/max/is_* etc.
  2. Functions that operate on multiple data types, get_in/update_in etc.
  3. Functions that don’t fit anywhere else, like maybe apply
  4. Functions that work on single data types, like hd/length/elem/map_size

Now category #4 has always bothered me. Why are these functions not in modules, for example Tuple.elem/2? It is very surprising for a new user to see that the most important functions operating on tuples for example are not in Tuple. Even from category #3 some functions could be moved to other modules, like now some spawn functions are in Kernel and some are in Process.

If the reason is Kernel has all the functions that can be used in guards, I think that reasoning isn’t so useful because I still have to go look at the list of functions that I can use in guards as Kernel has other functions as well. I don’t remember the list offhand, so it won’t matter to me if it’s elem/2 or Tuple.elem/2 in the list.

Is there a technical reason for this mixed bag of stuff in Kernel? What does everyone else think about it? I don’t mean to come off as abrasive about it and it’s not the end of the world, it doesn’t come up that often when I program, but I’m just wondering about the reasoning behind it.

Showing Posts 1 to 10

kwando

kwando

One reason is that Kernel is imported everywhere by default and it contains core functions which other elixir constructs is built upon.

See the module documentation for Kernel

josevalim

josevalim

Creator of Elixir

Yup. As commented on the linked thread:

Correct. Kernel was used to be bigger but we shrunk it before 1.0. The consensus was to keep those in Kernel precisely because they are the only ones available in guards.

Two other things to consider about this:

  • If we allowed Map.size and Tuple.elem in guards, it would be extremely confusing to most why then we can’t use something like Map.get in guards. The lack of namespace reveals that there is something special about them. Today you at least know guards are a subset of Kernel. If we move them to modules, then any function that you have ever seen could be a guard.

  • I could easily argue in favor of the guard functions because of #1 too. Imagine how verbose guards would be if each call in a guard had to be fully qualified

I think a good exercise to accompany your question is “how would the language look like if most guards were in potentially separate modules?”. Would it more or less confusing? How easy would it be to discover guards? If you rewrite some existing codebases to the proposed syntax, is it better? Worse?

Of the functions you mentioned, the only ones I would consider misplaced are apply/3 and apply/2, now that we have the Function module (hindsight is 20/20), and potentially get_in and friends, although I would still wonder where would be a good placement for the latter.

Nicd

Nicd OP

Thanks for the explanation! :slight_smile:

This I have a different opinion on, though, as my only process to discover guards is “google for ‘elixir guards’ and read the list”. Since not all Kernel functions can be used in guards, the only guide I can use is the list of guard functions, and it wouldn’t matter then if they were in different modules. I do agree then reading a guard with Tuple.elem(tpl, 1) could lead to the confusion you mentioned, but finding out which functions are supported is already equally confusing.

Nicd

Nicd OP

Btw, we already have the X.y problem (not being a subset of Kernel) with defguard:

iex(1)> defmodule Foo do
...(1)>   defguard is_even(v) when is_integer(v) and rem(v, 2) == 0
...(1)> end
iex(2)> require Foo
iex(3)> defmodule Bar do
...(3)>   def baz(v) when Foo.is_even(v) do v + 1 end
...(3)>   def baz(v) do v - 1 end
...(3)> end
michallepicki

michallepicki

Possibly the Access module could be a good place for get_in etc

Eiji

Eiji

Personally I think that best way is to have Kernel.Guards which will contain delegates to other modules. Then we will have list of guards well documented in one module + all implementations will be in other modules like List or Map + we can automatically import Kernel.Guards as same as Kernel. I know that such changes will not come soon (if any), but I’m just curious what do you think about it @josevalim.

josevalim

josevalim

Creator of Elixir

There are a couple issues here:

  • Some (most) implementations will still need to be Kernel. For example, comparison/equality/arithmetic operators. Therefore, if we import both Kernel and Kernel.Guards, we will have conflicts
  • Having both elem/2 and Tuple.elem/2 means two ways of doing the same thing
  • The functions in Kernel.Guards are not available only in guards - this may be a documentation concern
Eiji

Eiji

I had in mind “normal” functions … I totally forgot about operators, sorry

For me it’s not a problem if we are using delegates - of course note that such function is delegated should exists in docs. It would be awesome if there would be a mark for it on summary functions list like: function(arg1, arg2) [delegate]

Then in Function section:

elem(tuple, index)                                                                                    [delegate]
elem(tuple(), non_neg_integer()) :: term()
This is delegated function for Tuple.elem/2

Personally I would like to see something like:


Kernel.Guards

This module groups special core functions which are allowed to be called inside guards, for example

# example defguard code here …

Nested guards

Guards allows also other guards to be called inside it, for example

# previous example
# example defguard which uses first example

Syntactic sugar in guards

Except functions grouped in this module and other guards we allow to use also arithmetic, comparison and equality operators which needs to be placed in Kernel module, for example:

# example defguard code with any operator

Old-way guards

Few words about macros here with a link to Macro module for more information …

Using guards

Few sentences about example usage in when and function body.

Summary

Summary content here …

Functions

Functions content here …


Of course I wrote it quickly, so this documentation will contain much more and much better text.

josevalim

josevalim

Creator of Elixir

It is not a concern implenentation wise. Just from the point of view of the usage of the language. When would you use elem/2 and when would you use Tuple.elem/2?

It feels like we are adding more categories and distinctions? How are they helpful? Why do we care if a guard is coming from Kernel or another module?

michalmuskala

michalmuskala

This has one important difference - you need to call require, because it’s a macro. Similar with things defined using defguard, they can’t be “just called”, you need to explicitly opt-in to using them with require. Can you imagine requiring all the Tuple and Map and others to use the guards? Each module would be littered with those.

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews