[erlang-questions] Proposal for is_iolist/1 guard

Oscar Hellström oscar@REDACTED
Tue May 26 16:12:07 CEST 2009


That's kinda what I'm getting at, without actually saying it out loud
(or typing...). Also, since it was about ports, the type I was thinking
about is iodata() which is defined as iolist() | binary().

So, to expand a bit. One reason you'd want to check this is in case you
could have something else in the list, which wouldn't make it an iolist.
Now the question I guess is how that element would end up in that list.
One thing that I have seen quite few times is when you get external data
which is turned in to a list of unicode code points. During testing this
might works fine, if the test data wouldn't produce any code points >
255. I guess what you'd like to do in this case is to traverse the
string a replace each code point with a multi-byte representation in
whatever encoding you want. In this case it would be highly ineffective
to first do this check, and in case you find a value in the list > 255,
do a second run on the data to do the encoding. On the other hand,
length() can also be quite inefficient in a guard...

In the other case, where the argument to the function is something
completely different, you could probably match this in another way. I
guess what I'm saying is that in most cases, it would be better to
ensure that you have valid data in some other way than using a guard for
it. I you potentially need to do some operation on the data, i.e. some
encoding, you're better of always running that.

These kind of guards has been discussed/asked for before (at least this
thread but searching would probably find more):
http://erlang.org/pipermail/erlang-questions/2005-March/014730.html
http://erlang.org/pipermail/erlang-questions/2005-March/014743.html
(same thread, but broken in the archives)

Robert Virding wrote:
> I wouldn't "like" (:-)) to but if I was testing for an iolist that is
> what I would have to do. Note that is_list/1 only tests the if
> argument is a cons cell or a [], it does not do a deep test.
>
> Robert
>
> 2009/5/26 Oscar Hellström <oscar@REDACTED
> <mailto:oscar@REDACTED>>
>
>     So if it's a list, you'd like to traverse the list and check if each
>     value is either a binary, another iolist or an integer >= 0 and < 256?
>
>     Robert Virding wrote:
>     > Unfortunately not, as an iolist can also be a nested list strucure
>     > containing bytes/characters and binaries.
>     >
>     > Robert
>     >
>     > 2009/5/26 Oscar Hellström <oscar@REDACTED
>     <mailto:oscar@REDACTED>
>     > <mailto:oscar@REDACTED
>     <mailto:oscar@REDACTED>>>
>     >
>     >     Hi Maxim,
>     >
>     >     Wouldn't is_list(X); is_binary(X) be enough?
>     >
>     >     Maxim Treskin wrote:
>     >     > Hello
>     >     >
>     >     > I see, erlang does not have is_iolist/1 guard, while
>     iolist() type
>     >     > used in some places in OTP (i.e. ports interaction).
>     >     > Is it possible to add this BIF?
>     >     >
>     >     > Thank you.
>     >     >
>     >     > --
>     >     > Maxim Treskin
>     >     >
>     >    
>     ------------------------------------------------------------------------
>     >     >
>     >     > _______________________________________________
>     >     > erlang-questions mailing list
>     >     > erlang-questions@REDACTED
>     <mailto:erlang-questions@REDACTED>
>     <mailto:erlang-questions@REDACTED
>     <mailto:erlang-questions@REDACTED>>
>     >     > http://www.erlang.org/mailman/listinfo/erlang-questions
>     >     Best regards
>     >
>     >     --
>     >     Oscar Hellström, oscar@REDACTED
>     <mailto:oscar@REDACTED>
>     >     <mailto:oscar@REDACTED
>     <mailto:oscar@REDACTED>>
>     >     Office: +44 20 7655 0337
>     >     Mobile: +44 798 45 44 773
>     >     Erlang Training and Consulting Ltd
>     >     http://www.erlang-consulting.com/
>     >
>     >     _______________________________________________
>     >     erlang-questions mailing list
>     >     erlang-questions@REDACTED
>     <mailto:erlang-questions@REDACTED>
>     <mailto:erlang-questions@REDACTED
>     <mailto:erlang-questions@REDACTED>>
>     >     http://www.erlang.org/mailman/listinfo/erlang-questions
>     >
>     >
>
>
>     --
>     Oscar Hellström, oscar@REDACTED
>     <mailto:oscar@REDACTED>
>     Office: +44 20 7655 0337
>     Mobile: +44 798 45 44 773
>     Erlang Training and Consulting Ltd
>     http://www.erlang-consulting.com/
>
>

Cheers

-- 
Oscar Hellström, oscar@REDACTED
Office: +44 20 7655 0337
Mobile: +44 798 45 44 773
Erlang Training and Consulting Ltd
http://www.erlang-consulting.com/




More information about the erlang-questions mailing list