tags:

views:

527

answers:

1

I'm trying to write a simple Thrift server in Erlang that takes a string and returns a string.

Everything seems to be working up to the point of calling my function:

handle_function(Function, Args) when is_atom(Function), is_tuple(Args) ->
case apply(?MODULE, Function, tuple_to_list(Args)) of
 ok -> ok;
 Reply -> {reply, Reply}
end.

test([X]) ->
"You sent: " ++ X.

I'm getting a function_clause. The stack trace shows the following:

{function_clause, [{server, test, [<<"w00t">>]},
{server,handle_function, 2}, ...

My handle_function is copied from the tutorial file so I won't be surprised if I need to tweak it. Any ideas?

+6  A: 

That last argument of apply should be a list of arguments to 'test', e.g., if tuple_to_list(Args) resulted in:

[1]

...then:

test(1)

If tuple_to_list(Args) resulted in:

[1,2]

...then:

test(1,2)

So, if {<<"woot">>} is being passed to tuple_to_list, that's going to be:

[<<"woot">>]

...so:

test(<<"woot">>)

...but test's signature asks for a list as the argument, so there's a mismatch.

mwt
Aha. I should have looked more closely at the docs for the apply function. I assumed since Args was a list that my function would need to receive one. Also, I needed to indicate a binary parameter a la: test(<<X/binary>>) ->
steamer25
How do you mean "indicate a binary parameter"?
rvirding
@rvirding In Erlang, a Thrift string is represented as binary data. My function had to receive <<X/binary>> and then decode it with binary_to_list(X).
steamer25