Anatolii Kosorukov
Anatolii Kosorukov

Reputation: 960

Describing a function in a list of parameters

I have a function description like that:

-spec match_evaluator(ReplaceFun, Text, Regex) -> Result
   when ReplaceFun :: function(),
        Text :: string(),
        Regex :: string(),
        Result :: string().

match_evaluator(ReplaceFun, Text, Regex) ->

I would like to add a more detailed description of the parameters of the parameter ReplaceFun. ReplaceFun is a link to a function.

Something like that:

-type replace_fun(string(),[string()]) :: {string(), non_neg_integer()}.
% : bad type variable

I would like to define this type correctly (a function with two parameters and return type). Please, tell me how to correctly describe the type of this function.

Upvotes: 1

Views: 47

Answers (2)

Anatolii Kosorukov
Anatolii Kosorukov

Reputation: 960

There is a -spec of the function match_evaluator:

-spec match_evaluator1(ReplaceFun, Text, Regex) -> Result
   when ReplaceFun :: fun((FullString :: string(), MatchResult :: [string()]) -> 
        (NewString :: string())),
        Text :: string(),
        Regex :: string(),
        Result :: string().

match_evaluator1(ReplaceFun, Text, Regex) ->
%..

The idea of the solution prompted me by @radrow, reference_manual and source code of Erlang standard library socket_test_evaluator.erl (which I found in the folder erlang/otp/erts/emulator/test).

Source code of this function is here.

Upvotes: 1

radrow
radrow

Reputation: 7159

You can write for example fun((string(), string()) -> string()) to refer a function that takes two strings and returns a string. If you don't care what are the parameters or the return type, then use any() in their places. I recommend a lecture of the Erlang documentation for more options.

Upvotes: 1

Related Questions