Skip to content

Instantly share code, notes, and snippets.

@djthread
Created December 16, 2020 02:56
Show Gist options
  • Save djthread/fc396d7ae6029ddcd20372b13abf877e to your computer and use it in GitHub Desktop.
Save djthread/fc396d7ae6029ddcd20372b13abf877e to your computer and use it in GitHub Desktop.

Revisions

  1. djthread created this gist Dec 16, 2020.
    41 changes: 41 additions & 0 deletions weird.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    defmodule Foo do
    def bar do
    :bar
    end
    end

    defmodule Weird do
    @moduledoc """
    Illustrates a weird behavior I noticed which doesn't seem to make sense.
    Set `MIX_ENV` to `prod` and `mix release`. Then, try these:
    $ _build/prod/rel/weird/bin/weird eval "Weird.a()"
    Function is not exported! Why? :complete amazement:
    $ _build/prod/rel/weird/bin/weird eval "Weird.b()"
    Function is not exported! Why? :complete amazement:
    $ _build/prod/rel/weird/bin/weird eval "Weird.c()"
    Function is exported.
    """

    # nope
    def a do
    if function_exported?(Foo, :bar, 0),
    do: IO.puts("Function is exported."),
    else: IO.puts("Function is not exported! Why? :complete amazement:")
    end

    # thought maybe loading the app or this might help - no dice
    def b do
    Application.ensure_all_started(:weird)
    a()
    end

    # but after simply checking the functions on the module like this,
    # `function_exported?/3` returns true! By looking, I've changed it;
    # Heisenberg principle at work here!
    def c do
    Foo.__info__(:functions)
    a()
    end
    end