<div>That's what throws are for.<br></div><div><br></div><div><font face="courier new,monospace">init(Args) -></font></div><div><font face="courier new,monospace">    try check_opts(Args, [name, age, email]) of</font></div>

<div><font face="courier new,monospace">       ok -> {ok, #state{opts=Args}}</font></div><div><font face="courier new,monospace">    catch</font></div><div><font face="courier new,monospace">       throw:Reason={missing_opt, _} -> {stop, Reason}</font></div>

<div><font face="courier new,monospace">    end.</font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">check_opts(Options, ToCheck) -></font></div><div><font face="courier new,monospace">    F = fun(Opt, OptList) -></font></div>

<div><font face="courier new,monospace">             case proplists:get_value(Opt, OptList) of</font></div><div><font face="courier new,monospace">                undefined -> throw({missing_opt, Opt});</font></div><div>

<font face="courier new,monospace">                _ -> OptList</font></div><div><font face="courier new,monospace">             end</font></div><div><font face="courier new,monospace">    end,</font></div><div><font face="courier new,monospace">    lists:foldl(F,Options, ToCheck),</font></div>

<div><font face="courier new,monospace">    ok.</font></div><div><br></div><div>This uses a tiny trick with folds to check for all options in a list and make sure they're part of the list passed in. If any of them is missing, a throw is called, returned in a list and init fails.<font face="arial,helvetica,sans-serif"></font></div>

<br><div class="gmail_quote">On Wed, Nov 23, 2011 at 9:50 AM, Joel Reymont <span dir="ltr"><<a href="mailto:joelr1@gmail.com">joelr1@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

I'm passing a proplist to the init function of my gen_server.<br>
<br>
I need to ensure various options are present and validate them.<br>
<br>
I also don't want to have a huge nested case statement.<br>
<br>
Is it acceptable to throw or error when I cannot find or validate an option?<br>
<br>
Are there better ideas?<br>
<br>
N.B. The option values are assigned to fields of the gen_server state records.<br>
<br>
--------------------------------------------------------------------------<br>
- for hire: mac osx device driver ninja, kernel extensions and usb drivers<br>
---------------------+------------+---------------------------------------<br>
<a href="http://wagerlabs.com" target="_blank">http://wagerlabs.com</a> | @wagerlabs | <a href="http://www.linkedin.com/in/joelreymont" target="_blank">http://www.linkedin.com/in/joelreymont</a><br>
---------------------+------------+---------------------------------------<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br>