Skip to content

Instantly share code, notes, and snippets.

@shamshirz
Created January 18, 2023 21:32
Show Gist options
  • Save shamshirz/7d1389b40056c91030998a93d7b6c0f2 to your computer and use it in GitHub Desktop.
Save shamshirz/7d1389b40056c91030998a93d7b6c0f2 to your computer and use it in GitHub Desktop.
Example of Elixir Compilation Dependencies
defmodule A do
@moduledoc """
# Run yourself my doing a `mix new example` and then copying these files in a.ex, b.ex, c.ex, c2.ex
# Start - show incremental compile works
* mix compile
# Add `elixirc_options: [verbose: true],` to mix.exs
* mix compile
# Change A, recompile
* mix compile
# Change B, Ask what folks think will happen
* mix compile
# Change C, Ask what folks think will happen
* :mind-blown:
* mix compile
# Change C2, Ask what folks think will happen
* :mind-blown: x2
* mix compile
# Show what the graph looks like (source means go down the tree, sink means go up!)
* mix xref graph --source lib/a.ex
# Last example - crazy if folks are interested, Add to A
# Function defined at compile time, same dependency tree
* Uncomment if statement, and comment the first 2 lines
* mix compile
* mix xref graph --source lib/a.ex
* iex -S mix
* > A. <tab, tab>
"""
@b B.new()
def new(), do: @b
# if B.new().id > 3 do
# def fxn, do: "big id"
# else
# def fxn, do: "small id"
# end
end
defmodule B do
@moduledoc "A Compile-time Dependency of A"
def new, do: C.new()
def new2, do: C2.new()
end
defmodule C do
@moduledoc "Runtime Dep of B"
defstruct [:id]
def new, do: %C{id: 4}
end
defmodule C2 do
@moduledoc "Runtime Dep of B"
def new, do: %{id: 3}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment