@@ -15,7 +15,7 @@ defmodule Code do
15
15
"""
16
16
17
17
@ doc """
18
- Returns all the loaded files.
18
+ Returns all loaded files.
19
19
"""
20
20
def loaded_files do
21
21
:elixir_code_server . call :loaded
@@ -32,32 +32,32 @@ defmodule Code do
32
32
end
33
33
34
34
@ doc """
35
- Appends a path to Erlang VM code path.
36
- The path is expanded with `Path.expand` before being appended.
35
+ Appends a path to the Erlang VM code path.
36
+ The path is expanded with `Path.expand/1 ` before being appended.
37
37
"""
38
38
def append_path ( path ) do
39
39
:code . add_pathz ( Path . expand to_char_list ( path ) )
40
40
end
41
41
42
42
@ doc """
43
- Prepends a path to Erlang VM code path.
44
- The path is expanded with `Path.expand` before being prepended.
43
+ Prepends a path to the Erlang VM code path.
44
+ The path is expanded with `Path.expand/1 ` before being prepended.
45
45
"""
46
46
def prepend_path ( path ) do
47
47
:code . add_patha ( Path . expand to_char_list ( path ) )
48
48
end
49
49
50
50
@ doc """
51
- Deletes a path from Erlang VM code path.
52
- The path is expanded with `Path.expand` before being deleted.
51
+ Deletes a path from the Erlang VM code path.
52
+ The path is expanded with `Path.expand/1 ` before being deleted.
53
53
"""
54
54
def delete_path ( path ) do
55
55
:code . del_path ( Path . expand to_char_list ( path ) )
56
56
end
57
57
58
58
@ doc """
59
- Evaluates the contents given by string. The second argument is the
60
- binding (which should be a keyword) , followed by a keyword list of
59
+ Evaluates the contents given by ` string` . The second argument is
60
+ a keyword list of variable bindings , followed by a keyword list of
61
61
environment options. Those options can be:
62
62
63
63
* `:file` - the file to be considered in the evaluation
@@ -71,28 +71,39 @@ defmodule Code do
71
71
* `:requires` - a list of modules required
72
72
* `:functions` - a list of tuples where the first element is a module
73
73
and the second a list of imported function names and arity. The list
74
- of function names and arity must be sorted;
74
+ of function names and arity must be sorted
75
75
* `:macros` - a list of tuples where the first element is a module
76
76
and the second a list of imported macro names and arity. The list
77
- of function names and arity must be sorted;
77
+ of function names and arity must be sorted
78
78
79
- Notice that setting any of the values above overrides Elixir default
79
+ Notice that setting any of the values above overrides Elixir's default
80
80
values. For example, setting `:requires` to `[]`, will no longer
81
81
automatically require the `Kernel` module; in the same way setting
82
82
`:macros` will no longer auto-import `Kernel` macros like `if`, `case`,
83
83
etc.
84
84
85
+ Returns a tuple of the form `{ value, binding }`,
86
+ where `value` is the the value returned from evaluating `string`; `binding`
87
+ is a keyword list with the value of all variable bindings after evaluating
88
+ `string`. If an error occurs while evaluating `string` an exception will be raised.
89
+
85
90
## Examples
86
91
87
92
iex> Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
88
- { 3, [ {:a, 1}, {:b, 2} ] }
93
+ {3, [a: 1, b: 2]}
94
+
95
+ iex> Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
96
+ {3, [a: 1, b: 2, c: 3]}
97
+
98
+ iex> Code.eval_string("a = a + b", [a: 1, b: 2])
99
+ {3, [a: 3, b: 2]}
89
100
90
- For convenience, you can pass `__ENV__` as an argument and
91
- all imports, requires and aliases will be automatically carried
92
- over:
101
+ For convenience, you can pass `__ENV__` as the `opts` argument and
102
+ all imports, requires and aliases defined in the current environment
103
+ will be automatically carried over:
93
104
94
105
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
95
- { 3, [ {:a, 1}, {:b, 2} ] }
106
+ {3, [a: 1, b: 2] }
96
107
97
108
"""
98
109
def eval_string ( string , binding // [ ] , opts // [ ] )
@@ -115,21 +126,20 @@ defmodule Code do
115
126
@ doc """
116
127
Evaluates the quoted contents.
117
128
118
- This function accepts a list of environment options.
119
- Check `Code.eval_string` for more information.
129
+ See `eval_string/3` for a description of arguments and return values.
120
130
121
131
## Examples
122
132
123
133
iex> contents = quote(hygiene: [vars: false], do: a + b)
124
134
...> Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
125
- { 3, [ {:a, 1}, {:b, 2} ] }
135
+ {3, [a: 1, b: 2] }
126
136
127
- For convenience, you can pass `__ENV__` as an argument and
128
- all options will be automatically extracted from the environment:
137
+ For convenience, you can pass `__ENV__` as the `opts` argument and
138
+ all options will be automatically extracted from the current environment:
129
139
130
140
iex> contents = quote(hygiene: [vars: false], do: a + b)
131
141
...> Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
132
- { 3, [ {:a, 1}, {:b, 2} ] }
142
+ {3, [a: 1, b: 2] }
133
143
134
144
"""
135
145
def eval_quoted ( quoted , binding // [ ] , opts // [ ] )
@@ -193,17 +203,17 @@ defmodule Code do
193
203
## Options
194
204
195
205
* `:file` - The filename to be used in stacktraces
196
- and the file reported in the __ENV__ variable.
206
+ and the file reported in the ` __ENV__` variable.
197
207
198
- * `:line` - The line reported in the __ENV__ variable.
208
+ * `:line` - The line reported in the ` __ENV__` variable.
199
209
200
- * `:existing_atoms_only` - When true, raises an error
210
+ * `:existing_atoms_only` - When ` true` , raises an error
201
211
when non-existing atoms are found by the tokenizer.
202
212
203
213
## Macro.to_string/1
204
214
205
215
The opposite of converting a string to its quoted form is
206
- `Macro.to_string`, which converts a quoted form to a string/binary
216
+ `Macro.to_string/1 `, which converts a quoted form to a string/binary
207
217
representation.
208
218
"""
209
219
def string_to_quoted ( string , opts // [ ] ) do
@@ -219,11 +229,11 @@ defmodule Code do
219
229
220
230
@ doc """
221
231
Converts the given string to its quoted form. It returns the ast if it succeeds,
222
- raises an exception otherwise. The exception is a TokenMissingError
232
+ raises an exception otherwise. The exception is a ` TokenMissingError`
223
233
in case a token is missing (usually because the expression is incomplete),
224
- SyntaxError otherwise.
234
+ ` SyntaxError` otherwise.
225
235
226
- Check `Code. string_to_quoted/2` for options information.
236
+ Check `string_to_quoted/2` for options information.
227
237
"""
228
238
def string_to_quoted! ( string , opts // [ ] ) do
229
239
file = Keyword . get opts , :file , "nofile"
@@ -239,14 +249,14 @@ defmodule Code do
239
249
@ doc """
240
250
Loads the given `file`. Accepts `relative_to` as an argument to tell where
241
251
the file is located. If the file was already required/loaded, loads it again.
242
- It returns a list of tuples { ModuleName, <<byte_code>> }, one tuple for each
252
+ It returns a list of tuples ` { ModuleName, <<byte_code>> }` , one tuple for each
243
253
module defined in the file.
244
254
245
255
Notice that if `load_file` is invoked by different processes
246
256
concurrently, the target file will be invoked concurrently
247
257
many times. I.e. if `load_file` is called N times with
248
258
a given file, the given file will be loaded N times. Check
249
- `require_file` if you don't want a file to be loaded concurrently.
259
+ `require_file/2 ` if you don't want a file to be loaded concurrently.
250
260
"""
251
261
def load_file ( file , relative_to // nil ) when is_binary ( file ) do
252
262
file = find_file ( file , relative_to )
@@ -258,16 +268,16 @@ defmodule Code do
258
268
259
269
@ doc """
260
270
Requires the given `file`. Accepts `relative_to` as an argument to tell where
261
- the file is located. The return value is the same as that of `load_file`. If
271
+ the file is located. The return value is the same as that of `load_file/2 `. If
262
272
the file was already required/loaded, doesn't do anything and returns nil.
263
273
264
274
Notice that if `require_file` is invoked by different processes concurrently,
265
275
the first process to invoke `require_file` acquires a lock and the remaining
266
276
ones will block until the file is available. I.e. if `require_file` is called
267
277
N times with a given file, it will be loaded only once. The first process to
268
- call `require_file` will get the list of loaded modules, others will get nil.
278
+ call `require_file` will get the list of loaded modules, others will get ` nil` .
269
279
270
- Check `load_file` if you want a file to be loaded concurrently.
280
+ Check `load_file/2 ` if you want a file to be loaded concurrently.
271
281
"""
272
282
def require_file ( file , relative_to // nil ) when is_binary ( file ) do
273
283
file = find_file ( file , relative_to )
@@ -298,17 +308,17 @@ defmodule Code do
298
308
299
309
Available options are:
300
310
301
- * `:docs` - when true, retain documentation in the compiled module,
302
- true by default;
311
+ * `:docs` - when ` true` , retain documentation in the compiled module,
312
+ ` true` by default;
303
313
304
- * `:debug_info` - when true, retain debug information in the compiled module.
314
+ * `:debug_info` - when ` true` , retain debug information in the compiled module.
305
315
This allows a developer to reconstruct the original source
306
- code, for such reasons, false by default;
316
+ code, for such reasons, ` false` by default;
307
317
308
- * `:ignore_module_conflict` - when true, override modules that were already defined
309
- without raising errors, false by default;
318
+ * `:ignore_module_conflict` - when ` true` , override modules that were already defined
319
+ without raising errors, ` false` by default;
310
320
311
- * `:warnings_as_errors` - cause compilation to fail when warnings are spewed ;
321
+ * `:warnings_as_errors` - cause compilation to fail when warnings are generated ;
312
322
313
323
"""
314
324
def compiler_options ( opts ) do
@@ -337,7 +347,7 @@ defmodule Code do
337
347
338
348
@ doc """
339
349
Ensures the given module is loaded. If the module is already
340
- loaded, it works as no-op. If the module was not loaded yet,
350
+ loaded, it works as no-op. If the module was not yet loaded ,
341
351
it tries to load it.
342
352
343
353
If it succeeds loading the module, it returns
@@ -352,9 +362,9 @@ defmodule Code do
352
362
are loaded as needed. In embedded mode the opposite happens, as all
353
363
modules need to be loaded upfront or explicitly.
354
364
355
- Therefore, this function is useful to check if a module is loaded
356
- before using it and react accordingly. For example, the `URI` module
357
- uses this function to check if a specific parser exists for a given
365
+ Therefore, this function is used to check if a module is loaded
366
+ before using it and allows one to react accordingly. For example, the `URI`
367
+ module uses this function to check if a specific parser exists for a given
358
368
URI scheme.
359
369
360
370
## Code.ensure_compiled
@@ -363,15 +373,15 @@ defmodule Code do
363
373
superset of `ensure_loaded/1`.
364
374
365
375
Since Elixir's compilation happens in parallel, in some situations
366
- you may need to use a module but it was not compiled yet, therefore
376
+ you may need to use a module but that was not yet compiled , therefore
367
377
it can't even be loaded.
368
378
369
- `ensure_compiled/1` puts a halt in the current process until the
379
+ `ensure_compiled/1` halts the current process until the
370
380
module we are depending on is available.
371
381
372
- In most of the cases, `ensure_loaded` is enough. `ensure_compiled`
373
- must be used just in same rare conditions , usually involving macros
374
- that needs to invoke a module for callback information.
382
+ In most cases, `ensure_loaded` is enough. `ensure_compiled`
383
+ must be used in some rare cases , usually involving macros
384
+ that need to invoke a module for callback information.
375
385
"""
376
386
def ensure_loaded ( module ) when is_atom ( module ) do
377
387
:code . ensure_loaded ( module )
@@ -388,7 +398,7 @@ defmodule Code do
388
398
@ doc """
389
399
Ensures the given module is compiled and loaded. If the module
390
400
is already loaded, it works as no-op. If the module was not
391
- loaded yet, it checks if it needs to be compiled first and just
401
+ loaded yet, it checks if it needs to be compiled first and
392
402
then tries to load it.
393
403
394
404
If it succeeds loading the module, it returns
0 commit comments