HomePage | RecentChanges | Preferences

Elixir Use

I ran across use Module.Name in Other People's Code and was not sure exactly what it meant.

There is nothing in the getting started docs. It seems like it should be on http://elixir-lang.org/getting-started/alias-require-and-import.html as it must be doing something similar.

Resorting to irc, I learn that I am looking in the wrong docs.

3:39 PM <wsmoak> Is 'use' documented anywhere?  It's impossible to search for... I'm guessing it makes all the functions of the `use`d module available without typing the prefix?
3:40 PM <ericmj> wsmoak: first result when searching for 'use' here http://elixir-lang.org/docs/stable/elixir/
3:42 PM <wsmoak> ah, I was on the site in the 'getting-started' pages and not finding it.
3:42 PM <asonge> wsmoak: btw, the only thing "use" does is invoke Module.__using__
3:43 PM <asonge> oh, and requires the module to invoke the macro
3:43 PM <wsmoak> trying to make sure I really understand Other People's Code before I go copying and pasting...
3:45 PM <wsmoak> thank you [again, as always] ericmj and asonge :)
3:45 PM <ericmj> happy to help

So the answer is at http://elixir-lang.org/docs/stable/elixir/Kernel.html#use/2 (remember those anchors don't work in Safari...) and then http://elixir-lang.org/docs/stable/elixir/Bitwise.html#__using__/1

use is a macro, and this

use Whatever, option: value

first expands to

require Whatever
  Whatever.__using__([option: value])

... then __using__ is another macro that expands and you can pass it :only_operators or :skip_operators [not sure about that yet, it seems to pass through things like async: true to the testing library.]

HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 19, 2015 1:41 pm by (diff)