What is the “pin” operator for, and are Elixir variables mutable?

前端 未结 3 740
失恋的感觉
失恋的感觉 2020-12-08 06:39

Currently trying to understand the \"^\" operator in Elixir. From the website:

The pin operator ^ can be used when there is no interest in rebinding

3条回答
  •  既然无缘
    2020-12-08 07:18

    The best way to understand Elixir's pin operator ^ is with relatable examples.

    Problem:

    Users are allowed to change their passwords before they do, they will have to provide a new password and their previous password.

    Solution:

    In a language like JavaScript, we can write a naive solution like so

    let current_password = 'secret-1';
    
    const params = {
      new_password: 'secret-2',
      current_password: 'secret-2'
    }
    
    if (current_password !== params.current_password) {
      throw "Match Error"
    }
    

    The above will throw a Match Error because the user's supplied password does not match their current password

    Using Elixir's pin operator we can write the above as

    current_password = 'secret-1'
    
    { new_password, ^current_password } = { 'secret-2', 'secret-2'}
    

    The above will also rais a MatchError exception

    Explanation:

    Use the pin operator ^ to pattern match against an existing variable's value. In the Elixir's example above, the variable new_password is bound to the first item in the tuple (Elixirs data structure represented with {}), rather than rebinding the current_password variable, we pattern match against its existing value.

    Now this example from Elixir's docs should make sense.

    iex(1)> x = 1
    1
    iex(2)> ^x = 1 # Matches previous value 1
    1
    iex(3)> ^x = 2 # Does not match previous value 
    ** (MatchError) no match of right hand side value: 2
    

提交回复
热议问题