Match patterns in the formal parameter of the function definition

Here is something that I saw in erlang code several times, but this is a difficult thing for google, and I can find this example (first block of code in the link below):

http://www.process-one.net/en/wiki/ejabberd_HTTP_request_handlers/

In the "head" of the definition of the function of the process / 2

process(_LocalPath = ["world"], _Request) ->

there is a pattern match with the first parameter / argument;

This works the same as the guard, so the next sentence will be executed only if the first argument passed to the / 2 process is a string "world" or is the "world" some sort of default argument? Or am I completely misunderstood / misunderstood?

+3
source share
3 answers

, . , , "".

+4

: _LocalPath = [ "world" ] "guard". "process" [ "world" ], .

: _LocalPath "" , .

+2

= , . . , , , . . .

_, _LocalPath, , , . , . , _, , .

- _, . , . .

I personally very rarely use variables starting with _, and prefer to use only _. I also feel that cluttering up templates with unnecessary things is Bad Thing, so I won’t use aliases for such documentation. I would write:

%% process(LocalPath, Request) -> ... .

process(["world"], _) ->

or perhaps an ad type if you prefer. I believe the code is shorter shorter and clearer.

+2
source

Source: https://habr.com/ru/post/1721626/


All Articles