[Solved] Understanding a comment from Porgramming Elixir >1.6

Hi there is a comment made in Programming Elixir > 1.6 by David Thomas, that I’m having some difficulty understanding.

I don’t really get what he means by this, in bold, the other text is to show the context

The full syntax of import is

import Module [, only:|except: ]

The optional second parameter lets you control which functions or macros are imported…

Alternatively, you can give only: one of the atoms :functions or :macros, and import will bring in only functions or macros.

This is from page 125 of the book.

What does the bolded line mean?

1 Like

I believe it means that only accepts :functions and will only import all functions but not macors. :macros will import all macros but not functions.

Small example

defmodule TestMod do
  import List, only: :functions

  def print() do
    IO.puts("#{first([1, 2])}")
  end
end
iex
Erlang/OTP 22 [erts-10.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("test.ex")
[TestMod]
iex(2)> TestMod.print()
1
:ok
iex(3)> 

Where List.first() doesn’t need the module name in front.

2 Likes

Ah, silly of me, when he said atoms :functions and :macros, I read it as “atoms, functions, and macros” so my thinking was that one could import atoms separately.

I had forgotten that keyword lists such as


import List, only: [ flatten: 1]

Allowed the atoms to be written with the colon location flipped.

2 Likes