Part- 7
Last updated
Last updated
In general, when you pass the quote expression to Macro.to_string
, it returns the actual code in a string format and we all knew this.
The weird thing is, it gives the output in a single line along with characters inside the string.
Check it down
To print the new lines, you pipe the string output from Macro.to_string
to IO.puts
like in the following code snippet. It gives the clean output by printing in new line.
Finding the loaded files
In elixir, we are having a definition loaded_files
in the module Code
used to find the loaded files.
Let’s see this.
Run the command iex
inside your terminal and call the function Code.loaded_files
.
It gives an empty list []
as output because we haven’t loaded any files so far.
Let’s create a file hello.ex
with a single function definition hello
which just prints a string “Welcome to the world” for the demo purpose.
Save the file after editing.
Now, make sure you are in the same directory of where the file exists and run the command
It opens the Elixir interactive shell by loading the hello.ex
file as well.
Now, you can see the loaded file by calling Code.loaded_files
. It outputs the path to the file.
That is one way of loading files. You can also load them on-fly in iex session
Code.load_file/1
Unlike loading files with iex hello.ex
, you can load them on-fly in iex
session with the help of Code.load_file "path/to/file"
.
Let us assume that we have a file hello.ex
in our current directory and open iex
session from the same directory.
@deprecated
You might be noticed the warnings of using deprecated functions in a library along with some useful hint text. Today, we build this from scratch.
Suppose, you have written a library and want to update the name of one function in your next build release and if the user tried to access the old function then you have to warn him of using deprecated function instead of updated one.
To see this in action, you need to create new mix project.
Let’s do that.
Next change the directory to the project created.
Now, edit the file lib/hello.ex
in the project with the following code
This file comprises of two modules Hello
and Printee
. The Printee
module comprises of two functions print/0
and show/0
. Here purposely, print/0
is considered as a deprecated function used inside the Hello
module.
The mix compiler automatically looks for calls to deprecated modules and emit warnings during compilation.
So, when you compile the project mix compile
, it gives a warning saying
“print/0 is deprecated use show/0”
like in the following screenshot.
As we all know defmodule
is used to create a module. In similar, Module.create/3
is also used to create a module.
The only difference in defining a module with Module.create/3
is the definition inside the module is quoted expression.
However, we can use the help of quote
to create a quoted expression.
We have to pass three parameters to Moduel.create
.
The first one is name of the module. The second one is module definition in quoted expression. The third one is location. The location is given in keyword list
like [file: String.t, line: Integer ]
or else you can just take the help of Macro.Env.location(__ENV__)
which returns the same.
location of loc
The above line of code gives the context of the module definition and collected in module_definition
. We can use this module_defintion
to pass as second parameter to Module.create/3
function.
Let’s put all together and see the magic
Creating another module Foo
with same module function
This is used in debugging purpose inside the function.
It is also very common to use IO.inspect/2
with binding()
, which returns all variable names and their values:
When fun/3
is invoked with :laughing
, "time"
it prints:
You can also give context to the binding
If the given context is nil *(by default it is), *the binding for the current context is returned.
We can convert any integer to charlist using Integer.char_list
. It can be used in two ways either passing the base the base value or not passing.
Let’s try with base and with out base.
Try your own names as well and find your value with base
With the help of Process.info
, we can extract the process information like linked processes etc…
It is used in two different ways.
Note: *We get the information if and only if process is alive*
Here, we try to create a process and linking it to the current process using spawn_link
and will try to extract only linked processes. With no surprise, it should give the self
output pid which is the current process in our case.
Consider that you need to pass a map to a function and you have to ensure certain keys in the map then allow it to use the function body.
You can achieve this using structs with @enforce_keys
attribute. But, you can still use the pattern matching to the keys of a map.
Here, we don’t care how many keys present inside the map but we need atleast two keys name
, blog
to be present inside the map with any values.
If you observer the screenshot, we tried to access the function by sending a map with single key parameter where it is not allowed then with two keys map and the keys are exactly pattern matched where it is allowed to use the function then evenutally tried with more keys still it worked.
Thanks to pattern matching.
Code formatting is easy now with the help of mix task mix format
. It will take care of all you care about cleaning and refactoring as well. It can assure a clean code base or simply universal code base which maintains some useful standards. This really saves a lot of time.
To know more about the codebase formatting, check out my recent article on
1.6](https://medium.com/blackode/code-formatter-the-big-feature-in-elixir-v1-6-0-f6572061a4ba)
which explains all about using mix format
task and its configuration in detail with screen shots of vivid examples.
is same as following
In similar fashion, you can alias your current module like
In general, with out using alias __MODULE__
, we have to type full module name like %User.Authentication{key: "key", token: ".."}
to define the struct but now simply %Auth{key: "key", token: ".."}
will be the better approach.
File Scrennshot Vim Editor
Deprecated Function Warning
module definition context