-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add eenv_appname:get_/1/2 [ENHANCEMENT] #3
Comments
New % eenv_router.beam
-module(eenv_router).
-export([get/2]).
-export([get_/2, get_/3]).
get(appname_1, Key) -> eenv_appname_1:get(Key);
get(appname_2, Key) -> eenv_appname_2:get(Key);
get(_, _) -> unload.
%% extra API
get_(appname_1, Key) -> eenv_appname_1:get_(Key);
get_(appname_2, Key) -> eenv_appname_2:get_(Key);
get_(_, _) -> unload.
get_(appname_1, Key, Default) -> eenv_appname_1:get_(Key, Default);
get_(appname_2, Key, Default) -> eenv_appname_2:get_(Key, Default);
get_(_, _,_) -> unload.
|
A better idea IMHO. The whole purpose of % eenv_appname.beam
-module(eenv_appname).
-export([get/1]).
-export([ip/0, port/0]).
-export([get_/1,get_/2]).
get(ip) -> {ok, "127.0.0.1"};
get(port) -> {ok, 8080};
get(_) -> undefined.
%% extra API
ip() -> "127.0.0.1".
port() -> 8080.
get_(ip) -> "127.0.0.1";
get_(port) -> 8080;
get_(_) -> undefined.
get_(ip, _) -> "127.0.0.1";
get_(port, _) -> 8080;
get_(_, Default) -> Default.
|
In my option:
ip() -> "127.0.0.1".
port() -> 8080. This assuming that the Module find function is more efficient than function clause match? Inconvenient: Caller will make sure the key is exist. I think this will too low level down for normal user.
The original method just have one atom argument. It make the search as simple as possible. The new method increase one argument, and no use most of time, Not sure the compile can optimize Change to new method is very simple, you can do it and add a small benchmark in PR is welcome~ |
Would it be possible to also generate
get_/1,get_/2
functions like these:The idea with
get_/1
is to return the value immediately, and avoid matching against the tuple{ok, Value}
in the caller.Second,
get_/2
will return adefault value
instead of gettingundefined
.As you may have noticed, I've added an underscore
_
suffix to these new calls to not clash with the originalget/1
.The text was updated successfully, but these errors were encountered: