- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,从文档中几乎可以清楚标题中的三个实体是什么,但不太清楚它们的目的是什么。
常量在许多语言中都很常见。你不想写3.14
遍及你的代码,这就是为什么你定义一个常量 PI
就它所代表的事物的性质而言,它是无法改变的,这就是为什么它的值(value)在时间上是恒定的。
使用 :=
定义绑定(bind)到另一个实体的变量也几乎清楚但不是真的。如果我将一个变量绑定(bind)到 3.14
, 和定义一个 PI
不一样吗?持续的?是 $a := $b
实际定义 $a
作为 $b
的别名或者,正如其他一些语言所说的,引用?此外,在某些语言中通常使用引用来明确说明,在对象构造或函数调用中,您不想复制对象,但为什么在相同范围内拥有
$number_of_cakes = 4;
$also_the_number_of_cakes := $number_of_cakes;
最佳答案
Constants are common in many languages. You don't want to write 3.14 all over your code and that's why you define a constant PI that, by the nature of the thing it represents, cannot be changed and that's why its value is constant in time.
constant
.
constant
仅在编译期间首次遇到时初始化一次,并且永远不会再次初始化,因此它的值在整个程序运行期间保持不变。编译器可以依赖它。读者也可以——只要他们清楚地理解“值(value)”在这种情况下的含义。1
BEGIN my $bar = 42;
loop {
constant foo = $bar;
$bar++;
last if $++ > 4;
print foo; # 4242424242
}
请注意,如果您没有
BEGIN
然后常量将被初始化并被值
(Any)
卡住。 .
Defining a variable bound to another entity with
:=
is also almost clear but not really. If I bind a variable to3.14
, isn't it the same as defining aPI
constant? Is$a := $b
actually defining$a
as an alias to$b
or, as some other languages call it, a reference?
my $a = 42;
my $b := $a;
$a++;
$b++;
say $a, $b; # 4444
Finally, the documentation explains how one can define a sigilless variable (that cannot be varied, so, actually, another kind of constant) but not why one would do that.
my $variable = 42; # puts the value 42 INTO $variable
my \variable = $variable; # binds variable to $variable
say ++variable; # 43
my \variable2 = 42; # binds variable2 to 42
say ++variable2; # Cannot resolve caller prefix:<++>(Int:D);
# ... candidates ... require mutable arguments
如果标识符绑定(bind)到不可变的基本标量值(例如
42
)或其他完全不可变的值(例如典型的
List
),我个人更喜欢削减符号,否则使用符号。 YMMV。
What problems do sigilless variables solve? What's a practical scenario when a variable, a constant, a binding variable do not solve the problem that a sigilless variable solve?
constant
with composite containers .
关于raku - Sigilless 变量、常量、绑定(bind) : when to choose what,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55210794/
因此,从文档中几乎可以清楚标题中的三个实体是什么,但不太清楚它们的目的是什么。 常量在许多语言中都很常见。你不想写3.14遍及你的代码,这就是为什么你定义一个常量 PI就它所代表的事物的性质而言,它是
我是一名优秀的程序员,十分优秀!