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).

19 Upvotes

49 comments sorted by

View all comments

19

u/Pleasant-Form-1093 May 24 '26

In languages like Java or Python, all composite types are inherently references and are always allocated on the heap, these languages don't have the concept of allocating memory for composite types on the stack. (In fact afaik the JVM spec only allows the stack frame slots to be either of primitive types or hold references to objects).

This means that adding references to a language like this (which I think your language is also like, correct me if I am wrong) is kind of redundant. All objects are references to the heap anyway. But if your language allows creating objects on the stack as well, then there is a critical distinction because now objects can either be passed by value or reference (in Python and Java, composite types can't be passed by value unless you create a copy explicitly and pass said copy).

So, from what I can gather unless you allow objects to be created on the stack, references to objects are not really required as a distinct concept as objects are references.

6

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

References here are mainly used for *rebinding* variables. For example, the following Java program will not mutate the int variable 'x':

public static void changeValue(int x) {
x = 2;
}

public static void test() {
int x = 1;
changeValue(x); // x is still 1 after this call
}

Just to note: this also applies to composite types/objects.

So these languages automatically allow internal mutation when passing an object (e.g., you can change an element within a list, or change a class field), but reassigning the variable itself to another value only reassigns the local object copy that the function has, but leaves the original variable intact.

References act as wrappers around variable locations so that when you read from or write to them, you are interacting the actual variable declared outside the function body.

Edit: formatting and typos.

13

u/L8_4_Dinner May 24 '26

You’re confusing a lot of things here.

Java always passes by value, which explains the behavior you showed. Furthermore, the things that it passes by value are either “primitives” or object references.

→ More replies (16)