引用特性是否可以完全去除 #19
Replies: 4 comments 3 replies
-
Wikipedia: Value type and reference typeReference types are represented as a reference to another value, which may itself be either a value or reference type. Reference types are often implemented using pointers, though many high-level programming languages such as Java and Python do not expose these pointers to the programmer. Reference types have identity, meaning that it is possible to distinguish two references at runtime, even when they contain underlying values that are equal. Essential C# 7.0Chapter 3: More with Data TypesReference TypesBy contrast. the value of a variable of reference type is a reference to a storage location that contains data. Reference types store the reference where the data is located instead of storing the data directly, as value types do. blog: rust-references-vs-pointersUltimately, the underlying representation is the same: both hold an address for some memory. The difference between them is ultimately in semantics. References have some rules enforced by the compiler. Specifically, they cannot outlive what they refer to (the "referent"), and mutable references cannot be aliased. Other than that, references behave a lot like the variables they point to. They have a type, and you can interact with that type to read it or (with mutable references) modify it. On the other hand, pointers are semantically more about the address. This means that when we interact with them, we'll be modifying the address (things like add will do pointer offsets instead of adding to the underlying value). When we print them, we don't print the underlying value—in fact, we cannot get to the underlying value at all without the unsafe keyword. Instead, we print out the address. the difference between references and pointers in Rust: they have the same underlying data, but different constraints and semantics with the compiler.正如rust-references-vs-pointers这篇博客所言,指针和引用的底层实现是相同的,不同点在于规则约束和编译器的语义。 在其他的语言描述中,指针类型被认为是不安全的。如rust文档中,是这样描述指针的:
在golang中,指针类型直接被编入unsafe包中:
其他语言中,引用类型自带语法糖,会隐性解引用(implicit de-reference)。这一点同样起到保护对象数据的作用。 |
Beta Was this translation helpful? Give feedback.
-
在参考部分资料后,我并不认为,引用是历史包袱。相反,引用是出于保护数据(可以参考c中的union类型,数据操纵可以精确到bit)的考虑而被设计出来的。 |
Beta Was this translation helpful? Give feedback.
-
这个问题可以持续关注, 如果遇到必须使用引用的场景再讨论 |
Beta Was this translation helpful? Give feedback.
-
https://nim-lang.org/docs/manual.html#types-reference-and-pointer-typesReferences (similar to pointers in other programming languages) are a way to introduce many-to-one relationships. This means different references can point to and modify the same location in memory (also called aliasing). Nim distinguishes between traced and untraced references. Untraced references are also called pointers. Traced references point to objects of a garbage-collected heap, untraced references point to manually allocated objects or objects somewhere else in memory. Thus, untraced references are unsafe. However, for certain low-level operations (accessing the hardware) untraced references are unavoidable. Traced references are declared with the ref keyword, untraced references are declared with the ptr keyword. In general, a ptr T is implicitly convertible to the pointer type. |
Beta Was this translation helpful? Give feedback.
-
般若目前把类似C++的引用传递, 引用变量, 返回值引用类型等都取消了.
时候存在必须使用引用更为合适的场景?
Beta Was this translation helpful? Give feedback.
All reactions