Is there a language that allows you to manipulate primitives?

In most cases, languages ​​will not allow manipulating references to primitives. For instance:.

var a = 0; var b = a; // value is copied b++; // b now represents a new value as this is really b = b + 1; so a != b 

While manipulating non-primitives will result in manipulation of the state (sometimes destructive), which is reflected in all variables (using JS):

 var a = []; var b = a; // b is now a reference to the value stored in aapush(1); // b[0] is now 1 -- b and a are pointing to the same thing. 

It makes sense. I can fully understand why things like String.replace return a value instead of doing state manipulations.

I was interested, however, if there are no languages ​​that would allow primitives to manipulate states. For instance:.

 var a = 0; var b = a; // b references a b++; // b and a are now = 1. 

I know the index in lower-level languages, and it almost does what I'm talking about, but I feel that it only resets the value, and not actually updates the link.

I also know about PHP links, but since PHP doesn't allow these things:

 $str = "abcd"; $st[0] = "q"; // this causes an error. 

In addition, when concatenating a series of lines in PHP, the $str .= 'var' loop is implied to create new lines for each iteration.

Maybe I'm crazy, even thinking about it, but with the increasing prevalence of object models as a background for variables, it seems that it can exist (it seems, in a way, it is insanely complicated, especially if you allowed int , but it seems like this syntax would be a good source of training).

+4
source share
1 answer

Contrary to what you seem to suggest, this is not a new idea or obscurity. Lower-level languages ​​do not bother to force immunity of any type (after all, ++i will not exist or be wasteful otherwise - and the hardware does not have constant registers, right?), But they will also prefer (i.e. a = b copies the value, and does not quietly issue a link to the same value), so you should catch your hands and say that it refers to the same value twice, for example using pointers. In C:

 int x = 0; int *p = &x; *p = 1; /* x == 1 */ 

Similarly, C ++ has equally powerful pointers, as well as links that, for these purposes, work as pointers that are implicitly dereferenced (and cannot be changed to point to anything else, and cannot be NULL):

 int x = 0; int &y = x; y = 1; // x == 1 

In any case, it simply did not occur to anyone that the primitive types were immutable (why should they be such that Von Neumann machines all changed state), pointers lose most of their value if you cannot change the specified value, and banning pointers to certain mutable types would be a meaningless and severe restriction.

+4
source

All Articles