Nicd

Nicd

Purpose of Kernel

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.

Most Liked

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.

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.

Nicd

Nicd

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.

Where Next?

Popular in Discussions Top

Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement