<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 25 Oct 2017, at 15:04, Roger Lipscombe <<a href="mailto:roger@differentpla.net" class="">roger@differentpla.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">On 25 October 2017 at 13:29, Benoit Chesneau <<a href="mailto:bchesneau@gmail.com" class="">bchesneau@gmail.com</a>> wrote:<br class=""><blockquote type="cite" class="">I'm temped to compile dynamically a module for some highly demanded resources<br class="">to share their config instead of using ETS for it. But I'm wondering if's not<br class="">to hackish. (I'm also tempted by just using something more pure like sharing it<br class="">via message passing).<br class=""><br class="">Are other people do such thing?<br class=""></blockquote><br class="">Yes: <a href="https://github.com/mochi/mochiweb/blob/master/src/mochiglobal.erl" class="">https://github.com/mochi/mochiweb/blob/master/src/mochiglobal.erl</a><br class=""></div></div></blockquote><br class=""></div><div><br class=""></div><div>Indeed :) I’ve also somewhat simpler code:</div><div><br class=""></div><div><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Menlo';font-size:9.0pt;" class="">-include_lib(<span style="color:#6a8759;" class="">"syntax_tools/include/merl.hrl"</span>)<span style="color:#cc7832;font-weight:bold;" class="">.<br class=""></span><span style="color:#cc7832;font-weight:bold;" class=""><br class=""></span><span style="color:#808080;" class="">%% @doc Utility that converts a given property list into a module that provides<br class=""></span><span style="color:#808080;" class="">%% constant time access to the various key/value pairs.<br class=""></span><span style="color:#808080;" class="">%%<br class=""></span><span style="color:#808080;" class="">%% Example:<br class=""></span><span style="color:#808080;" class="">%%<br class=""></span><span style="color:#808080;" class="">%% load_config(store_config, [{backends, [{rocksdb_ram, barrel_rocksdb},<br class=""></span><span style="color:#808080;" class="">%% {rocksdb_disk, barrel_rocksdb}]},<br class=""></span><span style="color:#808080;" class="">%% {data_dir, "/path/to_datadir"}]).<br class=""></span><span style="color:#808080;" class="">%%<br class=""></span><span style="color:#808080;" class="">%% creates the module store_config:<br class=""></span><span style="color:#808080;" class="">%% store_config:backends(). => [{rocksdb_ram,barrel_rocksdb},{rocksdb_disk,barrel_rocksdb}]<br class=""></span><span style="color:#808080;" class="">%% store_config:data_dir => "/path/to_datadir"<br class=""></span><span style="color:#808080;" class="">%%<br class=""></span>-spec load_config(atom(), [{atom(), any()}]) -> ok<span style="color:#cc7832;font-weight:bold;" class="">.<br class=""></span>load_config(<span style="color:#9876aa;" class="">Resource</span>, <span style="color:#9876aa;" class="">Config</span>) <span style="color:#cc7832;font-weight:bold;" class="">when </span>is_atom(<span style="color:#9876aa;" class="">Resource</span>), is_list(<span style="color:#9876aa;" class="">Config</span>) -><br class=""> <span style="color:#9876aa;" class="">Module </span>= ?<span style="color:#9876aa;" class="">Q</span>(<span style="color:#6a8759;" class="">"-module(" </span>++ atom_to_list(<span style="color:#9876aa;" class="">Resource</span>) ++ <span style="color:#6a8759;" class="">")."</span>),<br class=""> <span style="color:#9876aa;" class="">Functions </span>= lists:foldl(<span style="color:#cc7832;font-weight:bold;" class="">fun</span>({<span style="color:#9876aa;" class="">K</span>, <span style="color:#9876aa;" class="">V</span>}, <span style="color:#9876aa;" class="">Acc</span>) -><br class=""> [make_function(<span style="color:#9876aa;" class="">K</span>,<span style="color:#9876aa;" class="">V</span>) | <span style="color:#9876aa;" class="">Acc</span>]<br class=""> <span style="color:#cc7832;font-weight:bold;" class="">end</span>,<br class=""> [], <span style="color:#9876aa;" class="">Config</span>),<br class=""> <span style="color:#9876aa;" class="">Exported </span>= [?<span style="color:#9876aa;" class="">Q</span>(<span style="color:#6a8759;" class="">"-export([" </span>++ atom_to_list(<span style="color:#9876aa;" class="">K</span>) ++ <span style="color:#6a8759;" class="">"/0])."</span>) <span style="color:#cc7832;font-weight:bold;" class="">|| </span>{<span style="color:#9876aa;" class="">K</span>, <span style="color:#9876aa;" class="">_V</span>} <- <span style="color:#9876aa;" class="">Config</span>],<br class=""> <span style="color:#9876aa;" class="">Forms </span>= lists:flatten([<span style="color:#9876aa;" class="">Module</span>, <span style="color:#9876aa;" class="">Exported</span>, <span style="color:#9876aa;" class="">Functions</span>]),<br class=""> merl:compile_and_load(<span style="color:#9876aa;" class="">Forms</span>, [verbose]),<br class=""> ok<span style="color:#cc7832;font-weight:bold;" class="">.<br class=""></span><span style="color:#cc7832;font-weight:bold;" class=""><br class=""></span>make_function(<span style="color:#9876aa;" class="">K</span>, <span style="color:#9876aa;" class="">V</span>) -><br class=""> <span style="color:#9876aa;" class="">Cs </span>= [?<span style="color:#9876aa;" class="">Q</span>(<span style="color:#6a8759;" class="">"() -> _@V@"</span>)],<br class=""> <span style="color:#9876aa;" class="">F </span>= erl_syntax:function(merl:term(<span style="color:#9876aa;" class="">K</span>), <span style="color:#9876aa;" class="">Cs</span>),<br class=""> ?<span style="color:#9876aa;" class="">Q</span>(<span style="color:#6a8759;" class="">"'@_F'() -> []."</span>)<span style="color:#cc7832;font-weight:bold;" class="">.<br class=""></span><span style="color:#cc7832;font-weight:bold;" class=""><br class=""></span><span style="color:#808080;" class="">%% @doc unload a config module loaded with the `load_config/2' function.<br class=""></span>-spec unload_config(atom()) -> true | false<span style="color:#cc7832;font-weight:bold;" class="">.<br class=""></span>unload_config(<span style="color:#9876aa;" class="">Resource</span>) -><br class=""> <span style="color:#9876aa;" class="">_ </span>= code:purge(<span style="color:#9876aa;" class="">Resource</span>),<br class=""> code:delete(<span style="color:#9876aa;" class="">Resource</span>)<span style="color:#cc7832;font-weight:bold;" class="">.</span></pre><div class=""><br class=""></div></div>which is doing something similar but create a function for each keys in the proplists. But I’m wondering if there are some cons to it apart the fact it is designed for case where little changes happen to the conf . It seems lot of people are using ets to share a config generally. Maybe becauseit feels a little hackish ?<div class=""><br class=""></div><div class="">- benoit</div></body></html>