Pass By Ref Breaks Type Neutrality

last modified: December 27, 2006

Maybe I am looking at this wrong, but there is something that bothers me about pass-by-reference. Ideally a language should be type-neutral or type-blind. It should treat all types or objects the same. However, many popular languages don't because they will pass "primitive" types by value but "complex" ones by reference (as a default). This seems to force a division into "primitive" and "complex", which the compiler/interpreter treats differently.

Thus, if one substitutes a complex type for a simple one, or vise versa, they will get different behavior. For example, suppose it is decided to use a delimited string list ("1,2,3,foo") instead of a vector array.

a = make_a_list({1, 2, 3},);
doSomethingToA(a);
display(a);
...
function doSomethingToA(object x) {
   x = appendElement(x, 4);
},

When "a" is a vector array, the function changes the original. However, if we use a delimited string instead, the function no longer changes the original.

But, it is not practical from a performance perspective to copy every type. Are we thus sacrificing type purity for performance?

Perhaps the mental work-around is to assign a property to each type such as "referenceMe". If "referenceMe" is True, then the type is passed by reference. Thus, one could create user-defined types that behave either way.


Loading...