[erlang-questions] jinterface List vs. String
Robert Raschke
rtrlists@REDACTED
Tue Dec 9 11:37:32 CET 2008
Hi Vlad,
thanks for the info.
On Tue, Dec 9, 2008 at 10:15 AM, Vlad Dumitrescu <vladdu55@REDACTED> wrote:
> Hi,
>
> On Tue, Dec 9, 2008 at 10:54, Robert Raschke <rtrlists@REDACTED> wrote:
>> does anyone have a good idea on how I can distinguish between a list
>> and a string in jinterface?
>
> The short answer is that you can't without traversing the list/string.
> Since there is no string datatype in Erlang, the two are only
> differentiated by their contents.
>
>> Or, in other words, if jinterface has decided to receive an
>> OtpErlangString (from a sent list like [1, 2, 3], for example), is
>> there an easy way to convert it to an OtpErlangList? I know I can do
>> it by hand, but that's ever so slightly icky.
>
> Actually, it is Erlang that decides to encode a list as a STRING_EXTor
> as a LIST_EXT, as an optimization.
>
> The way to handle that is by expecting either a list or a string and
> convert from each of them to the Java class you're going to use.
> Slightly icky, as you say...
>
> Maybe OtpErlangString should have inherited from OtpErlangList?
>
> best regards,
> Vlad
>
Maybe something like the following could be added to the OtpErlangString class?
/**
* Get this Erlang string as an Erlang list of Erlang bytes.
*
* @return the list of bytes contained in this string.
**/
public OtpErlangList asOtpErlangList() {
byte[] b = str.getBytes();
OtpErlangByte[] eb = new OtpErlangByte[b.length];
for (int i = 0; i < b.length; i++)
eb[i] = new OtpErlangByte(b[i]);
return new OtpErlangList(eb);
}
I haven't done Java in a while, maybe there's a way to "overload" the
cast operator? That would be another way.
Robby
More information about the erlang-questions
mailing list