TrailingDots

TrailingDots

"def" without "do"

In the source code I find this pattern repeated over and over.
As an example, in the elixir-1.5.3/lib/elixir/lib/enum.ex I find:

  @spec reduce(t, acc, reducer) :: result
  def reduce(enumerable, acc, fun)

The “def reduce…” has no “do” block. Almost all specs in this
file follow this pattern. Some do not.

Is this a “template” of some sort?

I’ve looked but can’t find an explanation.

What is this pattern called?

What changes when a spec has this and some specs do not?

Is this “def” without a “do” related to a “spec” at all? If not,
then what does this syntax provide?

Most Liked

NobbZ

NobbZ

It’s called a body less clause, and in this case used only to name params for the docs, as they are used only as patterns in the following clauses and the names given by the algorithm were not very good.

This kind of vodyless clause is optional.

If though you do use default arguments in functions that have multiple clauses a bodyless clause specifying the default arguments is mandatory.

OvermindDL1

OvermindDL1

You can only have one default clause per function, so if you have many heads then you put it on the bodyless clause.

TrailingDots

TrailingDots

Thanks! Now that I know the name, “bodyless clause”, I
found something about this bugger.

None of my Elixir books or searches of “def without do”
or similar guesses offered any illumination.

Thanks again! :slight_smile: :slight_smile:

lalo2302

lalo2302

It also helps the compiler when you have multiple definitions with default values. For example

    def foo(fn,acc)

    def foo(:ok, acc \\ []) do
         :ok 
    end

    def foo(:error, acc \\ nil) do
        :error
    end

Try it without the bodyless clause and it should display a warning. (It should, but I’m on mobile so I can’t be sure).

I don’t know exactly why, I just discovered it a few days ago.

TrailingDots

TrailingDots

Most interesting OvermindDL1.

I get warning messages that finally error out.

Interesting to try various compiler suggested mods.

I don’t thoroughly understand it either, but thanks for the tip. :slight_smile:

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement