FWIW, I don’t think it is too crazy to call it a “template” considering the same concept is called “template string” or “string template” in other languages.
Anyway, would using a function solve your problem? Using EEx seems like overkill if you just need to interpolate a string.
config :my_app, :general,
my_path123_template: fn var1 -> "/a/b/c/#{var1}/d" end
A template is usually a data structure, which can later be filled with data, so it‘s some kind of function or specialized string, with defined placeholders.
String interpolation happens on the spot and is basically just a different syntax to e.g. "abc" <> to_string(foo).
Using a callback in config might actually not work. Afaik only fully qualified calls like &MyApp.func/2 do actually work in config.
String interpolation happens on the spot and is basically just a different syntax to e.g. "abc" <> to_string(foo) .
I am not really sure what you mean by “happens on the spot” here.
A template is usually a data structure, which can later be filled with data, so it‘s some kind of function or specialized string, with defined placeholders.
This is pretty much describes string interpolation here, so I am not sure what point you are making.
Using a callback in config might actually not work. Afaik only fully qualified calls like &MyApp.func/2 do actually work in config.
It works, I don’t know why it wouldn’t.
~/Development/config_test
$ iex -S mix
Erlang/OTP 21 [erts-10.0.7] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Compiling 1 file (.ex)
Generated config_test app
Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> ConfigTest.run("hello world")
"this is interpolated: hello world"
"this is interpolated: hello world"
iex(2)> ConfigTest.run(234234)
"this is interpolated: 234234"
"this is interpolated: 234234"
The concern I’d have about the anonymous functions is whether they work in releases, but that’s just based on some vague memory about something I overheard somewhere.
The concern I’d have about the anonymous functions is whether they work in releases, but that’s just based on some vague memory about something I overheard somewhere.
Sure, I was mostly chose an anonymous function as a demonstration on how to defer the execution of the string until runtime.
~/Development/config_test (master●)
$ mix release
Compiling 1 file (.ex)
Generated config_test app
* assembling config_test-0.1.0 on MIX_ENV=dev
* skipping runtime configuration (config/releases.exs not found)
** (Mix) Could not read configuration file. It likely has invalid configuration terms such as functions, references, and pids.
Please make sure your configuration is made of numbers, atoms, strings, maps, tuples and lists. Reason: {3, :erl_parse, ['syntax error before: ', 'Fun']}
Exactly what I said, you can not persist an anonymous function in a config.
It might work when started and tested through mix as others have said, but also as @mhanberg has shown in the previous post, it won’t work for a release.
It seems as though functions in config are a somewhat common use case that we’re missing. Is there a strong technical reason to exclude functions from config that anyone is aware of?
I have also wanted to use short anonymous functions in config before.