r/ProgrammingLanguages • u/Big-Rub9545 • 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).
View all comments
Show parent comment
1
u/initial-algebra May 25 '26
These are called second-class references. They definitely work as a language feature, but in a dynamic language you have the additional challenge of not having static types to assist with automatically (de)referencing when appropriate. They are also more compatible with garbage collection, since anything pointed to by a second-class reference is guaranteed to be rooted as a global or on the stack (assuming no crazy control flow shenanigans), so they don't need to be traced (meaning they can point to individual fields/elements/variables without any problems) and they will never dangle. Though, if you have a moving GC, you will still need to relocate them somehow.