by Animats · 2y ago · 0 comments
Maybe that shouldn't even be exposed to the programmer. The programmer-level questions are, is it copyable, is it mutable, and is it alias-free? Whether it's passed as a copy or a pointer is really an issue for the compiler. If you're passed a read-only copy of something guaranteed to not be aliased, you can't tell the difference from a reference. Some Modula compilers made that decision automatically, based on object size.
Rust compilers have the info to do this. I'm sometimes asking myself whether I should pass, say, an array of 3 32-bit floats in graphics code by reference or by value. The compiler knows better than the programmer what the hardware can copy fast, and that may differ with the platform.
The problem here is the definition of "call by reference". In C++ that means being able to change the value outside of the function taking the reference.
void setByReference(float& v) { v = 123; }
float v;
setByReference(v);
cout << v; // prints 123
That feature of being able to pass by reference doesn't exist in say, JavaScript. you can only pass by value in JavaScript. The types of values in JS are undefined, null, boolean, number, string, reference-to-function, reference-to-object. You can never pass anything by reference, only by value.And that's where it gets confusing. If you have a variable who's value is a reference-to-object you pass the value. The value being "reference-to-object"
To re-iterate
const n = 1; fn(n); // call by value, type of value = number
const s = 'abc' fn(s); // call by value, type of value = string
const o = {} fn(o); // call by value, type of value = reference-to-object
In C++ though, you pass the a reference to the variable itself (in the example above). That's called call-by-reference.I can see why the OP feels it's muddled.
I still see even relatively experienced programmers who don't understand this, particularly working with Unity where a lot of programmers came from C++ to C# and don't realise for example that a C# function that takes a List 'by value' and modifies it is actually modifying the same instance that was passed in by the caller.
String s = "hello";
foo(s);
assert s == "hello";
In C++, if the function takes a non-const reference, you can't.Yes, Java always passes pointers to objects. But you can pass pointers by value. And passing a pointer by value is not the same as passing by reference!
I think the origin of the confusion around a function taking a list by value and the like is the implicitness of pointers in Java and its cousins. This Java method:
void foo(List<String> strings)
Is the equivalent of this C++ method: void foo(shared_ptr<List<string>> strings)
True systems languages make the pointers explicit.In C++ passing a pointer by value is effectively the same as passing a reference, the only real difference being that the syntax for accessing the underlying value is more implicit for a reference.
This is true, but aliasing makes it extremely difficult to automatically decide between reference and copy. For example, suppose you have a function that takes your array, and also a mutable slice:
fn f(a: [float32; 3], out: &mut [float32])
And now you call it with the same argument twice: let a = [1., 2., 3.];
f(a, &mut a)
The compiler cannot choose to pass a by reference here, since that would create simultaneous immutable and mutable references, which is forbidden by rust semanticsThere's the problem right there: any informal talk about programming concepts is bound to lead to confusion because informal language is open to interpretation (no pun intended). That's why we have formal definitions for these terms, as well as language specs which define how pass-by-value and pass-by-reference (if applicable) and pass-by-address (if applicable) work in a given language.
Comparing references isn't even an optimised version of comparing values, because when the references don't match but the values do, equality will be false.
I struggle to think of when reference equality would be useful to me. Like if I were writing a standard for-loop over an array, it's like I'd be comparing 'i' to 'i'.
If == gives me a fast 'yes' response, I feel like I (or my algorithm) should have already known they were going to be the same.
If == gives me a fast 'no' response, then do I not actually care about the value?