hannespetri

hannespetri

The documentation for supervisor child specification says the following:

The child specification contains 6 keys. The first two are required, and the remaining ones are optional:

  • :id - any term used to identify the child specification internally by the supervisor; defaults to the given module. In the case of conflicting :id values, the supervisor will refuse to initialize and require explicit IDs. This key is required.

(Source: Supervisor — Elixir v1.20.2)

How can something both be required and have a default value? Does “default” have a special meaning in Elixir that’s different from the mainstream one?

Showing Posts 1 to 6

peerreynders

peerreynders

Welcome to the forum!

Default just means that a value will be provided for you if you don’t explicitly give one (under certain conditions) - this is not in conflict with being required. The default ensures that there will always be a value.

I think the confusing bit is that default is usually used with optional arguments. One could argue that by providing a default value the argument is in fact no longer optional - what is optional is that you no longer have to be explicit about it’s value.

hannespetri

hannespetri OP

Thanks for your speedy answer.

I’m sorry, but I stil don’t get it. When is the default value used if a parameter always has to be supplied, overriding it? Furthermore, omitting the :id in a child specification gives me the following error:

** (MatchError) no match of right hand side value: {:error, {:start_spec, :missing_id}}
lib/supertest.ex:33: (module)
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6

peerreynders

peerreynders

Granted I haven’t tried it that way, what does your child spec look like?

children = [
  # The Stack is a child started via Stack.start_link([:hello])
  %{
    id: Stack,
    start: {Stack, :start_link, [[:hello]]}
  }
]

Note that the above is equivalent to

children = [
  {Stack, [:hello]}
]

defaults to the given module

Given that phrasing I would try:

children = [
  %{
    start: {Stack, :start_link, [[:hello]]}
  }

Hypothetically the module could be taken from the :start value - whether or not that is happening I don’t know.

If that fails I have to wonder whether “defaults to the given module” actually refers to the tuple notation rather than the child spec map - as in “the module name will be also be used as the id”.

The other possibility is that it is trying to convey the convention of using the module name as the id for a singleton process (which is what the tuple notation is doing).

hannespetri

hannespetri OP

Since I’m experimenting with the Stack example, the code you suggest is identical to the one that causes the above error message (should’ve posted my code, sorry!). However, the tuple version works just fine.

If that fails I have to wonder whether “ defaults to the given module ” actually refers to the tuple notation rather than the child spec map - as in " the module name will be also be used as the id ".

The other possibility is that it is trying to convey the convention of using the module name as the id for a singleton process (which is what the tuple notation is doing).

Both sound likely. Isn’t that quite misleading, though? If it’s a convention, then it should explicitly be described as such. If it refers to the tuple notation it’s not very helpful, because it hasn’t even been introduced at that point (it’s described many paragraphs further down.)

david_ex

david_ex

It is indeed a bit confusing.

Child specs (as in instances of the child_spec/0 type) require an :id value always.

However, writing a child spec by hand is error prone (not to mention a pain), and therefore there are functions to create/alter child specs for us: Supervisor — Elixir v1.20.2 It is in fact for this function that :id values are optional (i.e. don’t have to be given to the overrides) and for which a default value will be given if absent.

In other words, calling Supervisor.child_spec({MyModule, [foo: :bar]}) will result in a child spec with id MyModule, whereas calling Supervisor.child_spec({MyModule, [foo: :bar]}, id: :my_id) will return a child spec with id :my_id.

If you want to learn more about Supervisors, child specs, etc. you may find my blog series on the subject to be worth a read: http://davidsulc.com/blog/2018/07/09/pooltoy-a-toy-process-pool-manager-in-elixir-1-6/ (especially “customizing the child spec” at http://davidsulc.com/blog/2018/07/10/pooltoy-a-toy-process-pool-manager-in-elixir-1-6-part-1-9/).

hannespetri

hannespetri OP

If you want to learn more about Supervisors, child specs, etc. you may find my blog series on the subject to be worth a read: http://davidsulc.com/blog/2018/07/09/pooltoy-a-toy-process-pool-manager-in-elixir-1-6/ (especially “customizing the child spec” at http://davidsulc.com/blog/2018/07/10/pooltoy-a-toy-process-pool-manager-in-elixir-1-6-part-1-9/).

Thanks! Just began reading, but it looks very promising. That’s the thoroughness I wish all tutorials possessed.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews