Home > OS >  True or false: in a OO language that does not have pointers, all objects are references
True or false: in a OO language that does not have pointers, all objects are references

Time:12-21

Is it true that, in an object-oriented language that does not have explicit pointers, all objects must be references.

Here a reference is like SO defines in the tag :

A reference is a value that enables a program to indirectly access a particular datum, such as a variable or a record, in the computer's memory or in some other storage device.

Here is my reasoning. Consider what happens if we pass a data structure as an argument to a function. That data structure may be large, so we definitely don’t want a copy to be created. In a language that has explicit pointers, we would pass a pointer to the data structure. In a language that does not have explicit pointers, the argument better behave as a pointer implicitly, and provide indirect access to the data structure. In other words, it better be a reference.

A reply is needed only if the statement with which I began the question is false. In that case, I and the future readers would be helped by an explanation of why the above reasoning is not sound.

CodePudding user response:

I think that "[...] all objects must be references" is wrong for two reasons, the first of which might be nit-picky:

  1. objects aren't references or pointers. They might be referenced using references and pointers, but they themselves are collections of data (and sometimes code). This reason can be fixed by rephrasing the question as "all objects are handled via references" or something similar.
  2. "[...] we definitely don’t want a copy to be created." is a reasonable assumption for a general-purpose language, but some languages are very specialized and might exist in a niche where the drawbacks of copying large objects for each method/function call are considered worthwhile.

Fundamentally I think that all object-oriented languages will require support for some kind of reference to their objects. Whether that support comes in the form of simple pointers or something closer to C references is up to the language.

CodePudding user response:

The statement itself is a category error (an object is not a reference), but tweaking it to the most likely meaning:

all objects must be [handled by] references [in the implementation]

Then it is demonstrably False.

There are implementations of object oriented languages that simplify "reference types" to "value types" when it can be proven not to make a semantic difference. One such technique is tagged pointers which folds data into pointer/reference; this is used by the PyPy Python runtime and V8 JavaScript runtime to implement integer objects as plain values, for example. This means the implementation handles some objects by reference and some objects by value.

Generally, any non-recursive, immutable object can be implemented without references. Whether this is advantageous in an individual case ("we definitely don’t want a copy") is up to the runtime/compiler to decide.

  • Related