Simultaneous binding of a tuple and its destroyed elements in Erlang

I destroy a tuple and bind its elements to variables in the signature of an Erlang function like:

store({X, Y}, State) ->
    ...

      

But sometimes I need to concatenate the original tuple and its contents. So far, I've handled this with an extra line of code:

store(Point, State) ->
  {X, Y} = Point,
  ...

      

But I am new to Erlang and I think it is naive. My question is inspired by the linking in Scala fetch:

case point@Point(x, y) => ???

      

Is there a better way to bind the tuple and its contents at the same time, or is it best to destroy the tuple in a separate job?

+3


source to share


2 answers


You can write

store({X, Y} = Point, State) ->
  ...

      



which works similarly to the Scala example.

+14


source


If you are using

store({X, Y}, State) -> ...

In this case, when you call a function of the type mod:store({x, y, z}, state)

, it throws an exception error: no function clause matching mod:store{x, y, z}, state)

:;



if you use

store(Point, State) -> {X, Y} = Point, ...

In this case, when you call a function of the type mod:store({x, y, z}, state)

, it throws an exception error: no match of right hand side value {x, y, z} in function mod:store/2

:;

+2


source







All Articles