Please give this version a try and help us ensure Elixir v1.14.0 won’t have any regressions and work as expected for everyone! To try it out, you may need to either use a package manager or compile from source.
if Version.match?(System.version(), ">= 1.14.0-dev") do
@color_aliases Application.compile_env(:bunt, :color_aliases, [])
else
@color_aliases Application.get_env(:bunt, :color_aliases, [])
end
if Version.match?(System.version(), ">= 1.14.0-dev") do
import Bitwise
else
use Bitwise
end
… give me compiler warnings on the v1.14.0-rc.1:
==> bunt
Compiling 2 files (.ex)
warning: Application.get_env/3 is discouraged in the module body, use Application.compile_env/3 instead
lib/bunt_ansi.ex:300: Bunt.ANSI
Generated bunt app
==> bunt
Compiling 2 files (.ex)
warning: Application.get_env/3 is discouraged in the module body, use Application.compile_env/3 instead
lib/bunt_ansi.ex:300: Bunt.ANSI
Generated bunt app
Please note that the warnings concern code paths that are not executed due to the version check.
Is this supposed to happen? If so, how are library authors supposed to handle backwards compatibility going forward?
It would be absolutely amazing if we can solve this, so easy, but at the same time think how Elixir compiler could handle, for example:
if apply(Version, :match?, [System.version(), ">= 1.14.0-dev"]) do
@color_aliases Application.compile_env(:bunt, :color_aliases, [])
else
@color_aliases Application.get_env(:bunt, :color_aliases, [])
end
If I would be in such need then I would simply write a macro for it, for example:
defmodule MyLib do
defmacro __using__(_opts \\ []) do
if Version.match?(System.version(), ">= 1.14.0-dev") do
quote do
@color_aliases Application.compile_env(:bunt, :color_aliases, [])
end
else
quote do
@color_aliases Application.get_env(:bunt, :color_aliases, [])
end
end
end
end
defmodule MyApp do
use MyLib
end
With such macro there is no error, as always one of those quoted expressions is never used.
I’d be curious if the compiler could be smart enough if there would be a macro version to compare elixir version expectations and the condition would become if true | false do …