Skip to content

Instantly share code, notes, and snippets.

@apeiros
Last active April 5, 2018 08:39
Show Gist options
  • Save apeiros/bf3ddb3d332dc01ac43840e0d08b382d to your computer and use it in GitHub Desktop.
Save apeiros/bf3ddb3d332dc01ac43840e0d08b382d to your computer and use it in GitHub Desktop.
pass-by-object definition

pass-by-object

  • variables reference objects

      a = 1
      b = a

    b references 1, not a

    Corollary: it is not possible to change the reference of a variable indirectly

      a = 1
      b = a
      a = 2

    b still references 1

  • the value which is passed is the object, not the variable

      a = 1
      foo(a)

    In this example, the value 1 is passed, not the variable a.

  • a mutation of the object (if it is mutable) will be visible everywhere the object is referenced

      a = [1,2,3]
      b = a
      foo(a) # foo appens the value 4 to the array
      a # => [1,2,3,4] - a still references the same object, but the object has been mutated
      b # => [1,2,3,4] - since b references the same object as a, the mutation is visible there too
  • it's not possible to change which object a variable references from out-of-scope (e.g. after a = obj; foo(a), a will always still reference the same object as before the call)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment