<div dir="ltr">Thank you <span style="color:rgb(33,33,33)">Raimo for datailed responce.</span></div><br><div class="gmail_quote"><div dir="ltr">вт, 13 дек. 2016 г. в 12:42, Raimo Niskanen <<a href="mailto:raimo%2Berlang-questions@erix.ericsson.se">raimo+erlang-questions@erix.ericsson.se</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Tue, Dec 13, 2016 at 09:20:10AM +0000, Sergey Safarov wrote:<br class="gmail_msg">
> I want check type of passed arguments. In following example i can check<br class="gmail_msg">
> record type<br class="gmail_msg">
<br class="gmail_msg">
The term "check type" is misleading. In Erlang you select a code path<br class="gmail_msg">
depending on the type i.e checking when doing something with it. So if you<br class="gmail_msg">
have a function that does nothing but crash for the wrong type that might<br class="gmail_msg">
be regarded as a type checking function, but in Erlang you in general do<br class="gmail_msg">
not check the type until you do something with the term.<br class="gmail_msg">
<br class="gmail_msg">
Unless you want to check the type in an API to report errors as early as<br class="gmail_msg">
possible...<br class="gmail_msg">
<br class="gmail_msg">
><br class="gmail_msg">
> -record(person, {first_name, last_name}).<br class="gmail_msg">
> my_func(#person{} = Arg) -> Arg.<br class="gmail_msg">
><br class="gmail_msg">
> It works.<br class="gmail_msg">
> But how to do check of list of records? Like this<br class="gmail_msg">
><br class="gmail_msg">
> -record(person, {first_name, last_name}).<br class="gmail_msg">
> my_func([#person{} | ... ] = Arg) -> Arg.<br class="gmail_msg">
<br class="gmail_msg">
You will have to traverse the list to check all elements of the list. This<br class="gmail_msg">
is O(1) so in general you do not check a whole list just for fun. Instead<br class="gmail_msg">
you let it crash on invalid types while processing.<br class="gmail_msg">
<br class="gmail_msg">
Since processing a list is O(1) you loose just a constant factor by<br class="gmail_msg">
processing the list twice, so you could add a type checking pass over the<br class="gmail_msg">
list, but that is mostly just adding complexity... Just checking a record<br class="gmail_msg">
type still does not check the record fields nor contradictions between<br class="gmail_msg">
fields or any other problems with the records.<br class="gmail_msg">
<br class="gmail_msg">
my_func_check([#person{} | Persons]) -><br class="gmail_msg">
my_func_check(Persons);<br class="gmail_msg">
my_func_check([]) -><br class="gmail_msg">
ok.<br class="gmail_msg">
<br class="gmail_msg">
my_func(Arg) -><br class="gmail_msg">
my_func_check(Arg),<br class="gmail_msg">
...<br class="gmail_msg">
<br class="gmail_msg">
><br class="gmail_msg">
> Thanks<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
--<br class="gmail_msg">
<br class="gmail_msg">
/ Raimo Niskanen, Erlang/OTP, Ericsson AB<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
erlang-questions mailing list<br class="gmail_msg">
<a href="mailto:erlang-questions@erlang.org" class="gmail_msg" target="_blank">erlang-questions@erlang.org</a><br class="gmail_msg">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" class="gmail_msg" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="gmail_msg">
</blockquote></div>