<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I like this solution as well.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">The alternative is something like</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">my_fun(Arg) when is_string(Arg) -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> do_something;</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">my_fun(A) when is_atom(A) -></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> my_fun(atom_to_list(A)).</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">which coerces the atom to a string. But in my experience, this should be avoided unless the interface really demands that variance. It is better to crash the code and fix it in the caller rather than trying to coerce in the callee. Generally, if you are only accepting a small set of possible erlang terms (i.e., strings), then your code is more robust against sending wrong values. If you coerce/canonicalize terms, you run the risk of converting some value which shouldn't have been and you are now running a program where some data is undesired.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">The key is that `my_fun(atom_input)` is a bug in the program. It should be fixed by a programmer, and not saved by the system in an attempt to continue at all cost.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 4, 2020 at 9:41 AM WW <<a href="mailto:kingwang98@yahoo.com">kingwang98@yahoo.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:10px"><div dir="ltr">Hi I have a question about the error handling in erlang when input argument is in wrong format.</div><div dir="ltr"><br></div><div dir="ltr">example</div><div dir="ltr"><br></div><div dir="ltr">spec my_fun(Arg:String()) -> term().</div><div dir="ltr"><br></div><div dir="ltr">In case someone calling my_fun( atom_input), how to handle the error ?</div><div dir="ltr"><br></div><div dir="ltr">my solution:</div><div dir="ltr"><br></div><div dir="ltr">my_fun(Arg) when is_string(Arg) -> </div><div dir="ltr"> do_something.<br></div><div dir="ltr"><br></div><div dir="ltr">With the guards the function will only take string(), when input is not a string the interface will generate run_time error {error, function_fault}</div><div dir="ltr"><br></div><div dir="ltr"><br></div><div dir="ltr">My question: Is my solution good enough ? Any bettter way to handle the wrong input ?</div><div dir="ltr"><br></div><div dir="ltr"><br></div><div>Best Regards
W.W.(KingWang)</div></div></div></blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">J.</div></div>