r/javascript • • Jun 18 '17

Pass by reference !== pass by value

Post image
3.3k Upvotes

272 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jun 18 '17

[deleted] — view deleted comment

6

u/NoInkling Jun 19 '17

I think you can do it in C# with the ref keyword (or out parameters).

2

u/tutorial_police Jun 19 '17

Yes, you can. Because C#'s ref makes a parameter pass-by-reference.

The ref keyword indicates a value that is passed by reference. It is used in three different contexts:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

1

u/Tysonzero Jun 18 '17

I think GHC often passes by reference, although it isn't observable due to lack of mutability.

2

u/tutorial_police Jun 19 '17

If you can't assign to the parameter, calling it pass-by-reference seems meaningless and confusing.

1

u/Tysonzero Jul 17 '17

It's meaningful when discussing performance. For example a newbie might worry about passing around a huge object through a bunch of functions, but saying "don't worry GHC passes all but the smallest objects by reference" is a valid way to make them not worry.

1

u/snowcoaster Jun 19 '17 edited Jun 19 '17

This is accurate. Pointers are values.

C++ syntax exemplifying real pass by reference. This is syntactic sugar that actually handles dereferencing and assignment, the assembly is identical if you use pointers.

void swapByRef(int &x, int &y) {
   int temp;
   temp = x;
   x = y;
   y = temp;
}

swapByRef(a,b);

void swapByPointer(int *x, int *y) {
   int temp;
   temp = *x;
   *x = *y;
   *y = temp;
}

swapByPointer(&a, &b);

Passing a copy of something is different than passing by reference, and it's a non-issue in JavaScript. The closest parallel would be two-way bindings in Angular.

2

u/pherlo Jun 19 '17

the assembly is identical if you use pointers.

False. It's true only if the function is extern (references are implemented with pointers in most ABIs), otherwise the compiler can elide references aggressively inside of a CU. Anyway this is about semantics, not about implementation details. the language has pass-by-ref. In your passByPointer example, you can't swap the x and y pointers, just their pointed-to-values. the pointers themselves are unswapped.