是否有一种语言允许操作原语?
在大多数情况下,语言不允许对基元的引用进行操作。例如:
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
虽然对非基元的操作会导致(有时是破坏性的)状态操作,这会反映在所有变量中(使用 JS):
var a = [];
var b = a; // b is now a reference to the value stored in a.
a.push(1); // b[0] is now 1 -- b and a are pointing to the same thing.
这是完全有道理的。我完全可以理解为什么像 String.replace 这样的东西会返回一个值而不是执行状态操作。
不过,我想知道是否没有任何语言允许基元进行状态操作。例如:
var a = 0;
var b = a; // b references a
b++; // b and a are now = 1.
我知道低级语言中的指针,这几乎完成了我所说的,但我感觉它只是重新分配值而不是实际更新引用。
我也了解 PHP 引用,但由于 PHP 不允许这样的事情:
$str = "abcd";
$st[0] = "q"; // this causes an error.
此外,当在 PHP 中连接多个字符串时,$str .= 'var'
的循环据称会创建新字符串每次迭代。
也许我什至想知道这一点而疯狂,但随着对象模型作为变量背景的流行程度的增加,似乎这可能实际上存在(在某些方面似乎非常复杂,特别是如果您允许 int
对象被操纵,但似乎这样的语法将是一个很好的学习来源)。
In most cases, languages will not allow manipulations of references to primitives. Eg.:
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 manipulation of non-primitives will cause a (sometimes destructive) state manipulation which is reflected in all variables(using JS):
var a = [];
var b = a; // b is now a reference to the value stored in a.
a.push(1); // b[0] is now 1 -- b and a are pointing to the same thing.
This makes perfect sense. I can completely understand why things like String.replace will return a value instead of performing a state manipulation.
I was wondering, though, if there aren't any languages which allow primitives to have state manipulations. Eg.:
var a = 0;
var b = a; // b references a
b++; // b and a are now = 1.
I am aware of the pointer in the more low level languages, and that almost does what I'm talking about, but I get the feeling that it is only re-assigning the value and not actually updating a reference.
I also know about PHP references but since PHP does not allow things like this:
$str = "abcd";
$st[0] = "q"; // this causes an error.
Also, when concatenating a number of Strings in PHP, a cycle of $str .= 'var'
is purported to create new strings each iteration.
Perhaps I'm crazy for even wondering this, but with the increase in prevalence of object models as backgrounds for variables, it seems that this might actually exist (it seems insanely complicated in some ways, especially if you allowed for an int
object being manipulated, but it seems like such a syntax would be a good source of learning).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与您的假设相反,这既不是什么新想法,也不晦涩难懂。低级语言不会费心去强制执行任何类型的不变性(毕竟,
++i
不会存在,否则就会造成浪费 - 并且硬件也没有常量寄存器,对吧? ),但他们也会更喜欢值类型(即a = b
复制值,而不是默默地给出对同一值的引用),因此你必须亲自动手并告诉它引用两次使用相同的值,例如通过使用指针。在 C 中:类似地,C++ 具有同样强大的指针和引用,出于这些目的,它们的工作方式类似于隐式取消引用的指针(并且不能更改为指向其他任何内容,并且不能为 NULL):
在任何一种情况下,根本没有人想到让原始类型变得不可变(为什么要这样做?毕竟,冯·诺依曼机器都是关于改变状态的),如果你不能改变指向的值,指针就会失去很多价值,和禁止指向某些可变类型的指针将是毫无意义且严格的限制。
Contrary to what you seem to assume, this is neither a new idea nor obscure. Lower-level languages don't bother to enforce immutablity of any type (after all,
++i
wouldn't exist or be wasteful otherwise - and the hardware doesn't have constant registers either, right?), but they also will prefer value types (i.e.a = b
copies the value, not silently gives out a reference to the same value), so you have to get your hands dirty and tell it to refer to the same value twice, e.g. by using pointers. In C:Similarily, C++ has equally-powerful pointers and also references, which for these purposes work like pointers that are implicitly dereferenced (and can't be changed to point to anything else, and can't be NULL):
In either case, it simply didn't occur to anyone to make the primitive types immutable (why should they? Von Neumann machines are all about changing state, after all), pointers lose much of their value if you can't change the value pointed to, and disallowing pointers to certain mutable types would be a pointless and severe restriction.