2014-09-24 39 views
3

其解體的元素,我解構一個元組及其元素結合變量一個Erlang函數簽名,就像這樣:同時結合一個元組在二郎山

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

但有時我需要將源元組綁定其內容。到目前爲止,我已經用一行額外的代碼處理了這個問題:

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

但我是新來的Erlang,想知道這是否天真。

case [email protected](x, y) => ??? 

有沒有在同一時間的元組及其內容綁定一個更好的方式,或者是它最好的解構元組在一個單獨的作業:我的問題是通過在斯卡拉萃取結合的啓發?

回答

14

你可以寫

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

它的工作方式類似於Scala的例子。

2

如果使用

store({X, Y}, State) -> ... 在這種情況下,當你調用像mod:store({x, y, z}, state)的功能,它會引發一個異常: error: no function clause matching mod:store{x, y, z}, state);

而如果你使用

store(Point, State) -> {X, Y} = Point, ... 在這情況下,當你調用像mod:store({x, y, z}, state)這樣的函數時,會引發一個異常: error: no match of right hand side value {x, y, z} in function mod:store/2;