-
Notifications
You must be signed in to change notification settings - Fork 11
Use Application.get_env/3 calls instead of module attributes #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This was actually the main reason why I introduced the But you can recompile nanoid with |
Ah, I missed that. My bad. That's actually really interesting. It would probably help other developers to be aware of this limitation. Would you consider adding a note referencing/explaining this in the configuration section of the README? |
Most of developers introduce external libs for reducing mental burden. Because of that, I think it would be enough to tell them "how to do that", rather than "why to do that".
Create a new PR - #13 for "how to do that". |
But I'm sure that many developers would like to know the reason why a descision was made... |
This piqued my interest and I thought of :persistent_term as a possible solution. Here's the benchmark results based on 41c6285
5% slower for both is a lot better than the 14% reported in #6. Maybe that 5% hit is worth it to be able to change the defaults w/o recompiling? Maybe not 😉 |
Seems like newer Elixir version don't have that problem. I've created a new project, installed $ iex -S mix
==> nanoid
Compiling 4 files (.ex)
Generated nanoid app
==> nanoid_example
Compiling 1 file (.ex)
Generated nanoid_example app
Erlang/OTP 26 [erts-14.1.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
Interactive Elixir (1.15.7) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> Nanoid.generate
"F0BE-ZmwBL2g_ZC7Zj-qg"
iex(2)> byte_size Nanoid.generate
21 Then changed the configuration in import Config
config :nanoid, size: 10 And ran it again $ iex -S mix
==> nanoid
Compiling 4 files (.ex)
Generated nanoid app
==> nanoid_example
Generated nanoid_example app
Erlang/OTP 26 [erts-14.1.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
Interactive Elixir (1.15.7) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> Nanoid.generate
"gA7DmeUhRz"
iex(2)> byte_size Nanoid.generate
10 Note that |
Module attributes are defined in compile time. I just happened to download and compile the application (which includes compiling NanoID as a dependency) and then change the config in my app to use a different size by default.
However, since the
@default_size
module attribute was defined when NanoID compiled, it wasn't having any effect. I had to explicitly recompile NanoID.Instead of doing so, we can define the module attribute to be the fallback default value and use
Application.get_env/3
in runtime to get the value from the application config.Note: I made this separate to #7 since this is an issue that might not necessarily be merged.