[erlang-questions] throwing from gen_server:init/1

Dmitry Demeshchuk demeshchuk@REDACTED
Wed Nov 23 16:13:31 CET 2011


This seems a lot similar with a parse transform I've recently made. It
parses a proplist and converts it to a declared record.
However, I haven't implemented type checking yet. The goal is, for the
record like

-record(a, {
    foo :: integer(),
    bar :: binary()
}).

to automatically perform checks is_integer() and is_binary(). Also,
it's pretty easy to make fields mandatory if needed.

Take a look, maybe you'll be interested in helping with finishing this
parse transform:

https://github.com/doubleyou/ptrans

On Wed, Nov 23, 2011 at 7:05 PM, Joel Reymont <joelr1@REDACTED> wrote:
>
> On Nov 23, 2011, at 2:55 PM, Dmitry Demeshchuk wrote:
>
>> Can be a single try-catch instead. As far as I remember, sasl won't
>> show you proper error message if an exception occurs in init/1.
>
>
> Something like this then?
>
> 1> Opts1 = [{'foo', 1}, {'bar', 2}].
> [{foo,1},{bar,2}]
>
> 3> Opts2 = [{'foo', self()}, {'bar', 2}].
> [{foo,<0.34.0>},{bar,2}]
>
> 4> Opts3 = [{'foo', self()}, {'bar', 'baz'}].
> [{foo,<0.34.0>},{bar,baz}]
>
> 5> x:check(Opts1).
> {error,{invalid_option,foo}}
>
> 6> x:check(Opts2).
> {error,{invalid_option,bar}}
>
> 7> x:check(Opts3).
> {ok,{state,<0.34.0>,baz}}
>
> --- x.erl
>
> -module(x).
>
> -compile([export_all]).
>
> -record(state, {
>          foo :: pid(),
>          bar :: atom()
>         }).
>
> -spec check(proplists:proplist()) -> {'ok', #state{}} | {'error', _}.
>
> check(Opts) ->
>  try
>      Foo = ensure('foo', Opts, fun is_pid/1),
>      Bar = ensure('bar', Opts, fun is_atom/1),
>      {'ok', #state{ foo = Foo, bar = Bar }}
>  catch
>      error:X ->
>          {'error', X}
>  end.
>
> ensure(Key, Opts, Guard)
>  when is_atom(Key),
>       is_list(Opts),
>       is_function(Guard) ->
>    case proplists:get_value(Key, Opts) of
>        'undefined' ->
>            error({'missing_option', Key});
>        Value ->
>            case Guard(Value) of
>                'true' -> Value;
>                _      -> error({'invalid_option', Key})
>            end
>    end.
>
>
> --------------------------------------------------------------------------
> - for hire: mac osx device driver ninja, kernel extensions and usb drivers
> ---------------------+------------+---------------------------------------
> http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
> ---------------------+------------+---------------------------------------
>
>



-- 
Best regards,
Dmitry Demeshchuk



More information about the erlang-questions mailing list