[erlang-questions] Including function in configuration file

Grzegorz Junka list1@REDACTED
Mon Jul 9 23:37:12 CEST 2018


But you can specify here a function that you have written somewhere, 
then in that function read the configuration parameters that you want to 
use? For Erlang only the signature matters, i.e. that the function 
accepts the arguments  that the documentation says it should accept and 
that it returns the value it should return (e.g. boolean()). It's 
entirely up to you what the function does inside, e.g. it can execute 
some Java code, read a config file, or randomly return true/false.

What I was more interested is why do you think you need to specify 
anything in configuration related to those functions, why they can't be 
just static functions implemented somewhere.

Greg


On 09/07/2018 21:06, Code Wiget wrote:
> Hi Greg,
>
> I sent this earlier, but it must not have gone through to everyone. I 
> am trying to use special ssl options, for example:
>
> {reuse_session, fun()} {next_protocols_advertised, [binary()]}
>
> And
>
> {user_lookup_fun, {fun(), term()}}, {psk_identity, string()}, 
> {srp_identity, {string(), string()}}
>
>
> Which require functions as input. This means that I can’t use a 
> configuration file. Other members have suggested that I iterate over 
> my environment variables and replace the “fun()” with a function at 
> run time, rather than using a .config file. I think this is the most 
> straightforward approach.
>
>
> On Jul 9, 2018, 5:04 PM -0400, Grzegorz Junka <list1@REDACTED>, wrote:
>>
>> On 09/07/2018 18:57, Code Wiget wrote:
>>> Hi,
>>>
>>> Say I want to include a function as an environment variable
>>>
>>> I can do this:
>>>  application:set_env(App, Key, NewValue).
>>>
>>> Is there any way to do this from a .config file? Such as:
>>>  [{my_app, [ {my_fun, #Fun<my_app.12.39472874>}]}]
>>>
>>> — this gives a compile error.
>>>
>>
>> If you defined a function in the config how would you verify if it's 
>> correct? You will need to compile it first. In Erlang you can compile 
>> any arbitrary file directly in your application, see this for an example:
>> https://github.com/yoonka/migresia/blob/master/src/migresia_migrations.erl#L193
>>
>> So, theoretically, you could define a function as a string or binary 
>> in your config, then in the application write this to a file, compile 
>> into a binary (i.e. without writing the compiled module to a file) 
>> and then load directly from that binary and execute. But that sounds 
>> a bit weird and it's probably not what you need. Maybe you can just 
>> tell us what are you trying to accomplish?
>>
>> Best
>> Greg

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180709/bb0b48cc/attachment.htm>


More information about the erlang-questions mailing list