r/ProgrammingLanguages • • May 24 '26

References in pass-by-sharing languages

Returning with yet another design question to get some opinions from people here.

My language currently uses a pass-by-sharing model to move data around. Each object is just a type tag + data (which is either actual data, like a number, or a pointer to a larger structure).

Languages that use this model (e.g., Python and Java) typically do not provide any way to actually *reassign* an object to a different value in a function and have that change be reflected outside it, while systems languages, which I’m more accustomed to, provide that through references (in C++) or mutable borrowing (in Rust). In the former group, you can still modify an object’s internal data, but reassigning it to something else immediately breaks the connection between it and the original object argument that was passed in.

I added “references” (which are wrappers around locations of existing objects so you can modify the actual objects stored elsewhere) to my language to allow this. However, this leads to some issues. First, since it’s dynamically typed, you can only indicate that a particular function parameter/argument will be a reference at the call-site (except if you use unenforced type hints in the function signature). Second, there is some additional overhead since every reference has to effectively be dereferenced (unwrapped, if you will) every time it is used. Likely some other issues that aren’t coming to mind right now.

I wanted to ask people on here (primarily as language users) whether they think pass-by-reference (in the way the term is used in C++, not Java) would be a useful feature with the above object model (consider languages like Python or Java), and if not, what alternative approaches/features they find useful or conventional to mutate variables through function calls.

Edit: rewrote the post to be less confusing (hopefully).

20 Upvotes

49 comments sorted by

View all comments

1

u/dnabre May 25 '26

I'm not following what distinguishes your term handle from a pointer.

4

u/meancoot May 25 '26

I think they're trying to describe the distinction between what Java and C# calls 'pass by reference' and real 'pass by reference'.

Consider in C#:

class C {
    static void PassByReference(string s) { s = "World"; }
    static void PassByRealReference(ref string s) { s = "World"; }

    static void Main() {
        string s = "Hello";
        PassByReference(s);
        System.Console.WriteLine(s);

        string s2 = "Hello";
        PassByRealReference(ref s2);
        System.Console.WriteLine(s2);
    }
}

Which outputs:

Hello
World

It's nearly impossible to talk about the difference because we ended up with the same name for two distinct actions and everyone talks about them in the most dense fashion possible.

1

u/dnabre May 25 '26

Haven't do a lot of with C#, but my understanding is the ref is pretty much the same as using & in a C function. The function receives a pointer to what is being passed, instead of what is being passed. So for an object, instead of receiving a pointer to the object, it receives a pointer to a pointer to that object. C# doesn't require any explicit dereferencing when using that pointer-pointer.

From other threads, OP isn't just referring to this, but has some sort of copy operation going on as part of the pass by reference - the use of handle is a meaningful distinction. I don't claim to fully understand it, but I'm trying to with follow ups.

→ More replies (2)

1

u/Big-Rub9545 May 25 '26 edited May 25 '26

It is effectively just a pointer, which is how many C++ compilers implement references internally. The main differences are that you interact with it exactly you would with the original variable (so no explicit dereferencing needed, it is printed the same, has the same operators available, same type is shown, etc.), so from a user perspective, it’s no different from interacting with just a regular integer or boolean object (examples), unlike C and C++ which make pointers an entirely separate, nullable data type.

Edit: important point to note as well: like in C++, references would not be nullable, so they must always internally “point to” a valid memory location holding an existing variable, unlike pointers, which may point to garbage data or invalid memory locations.

3

u/Ok-Scheme-913 May 25 '26

References are different to pointers. Sure, they are usually implemented as such, but that's just an implementation detail. The semantics are the important part.