这是我以前见过的做法,但并不常见:在对值本身进行求值的同时将变量分配给一个值(或者是对表达式本身进行求值?)。例子:
// Outputs "The value is 1"
$value = 1;
if ($var = $value) {
echo "The value is $var";
}
Run Code Online (Sandbox Code Playgroud)
似乎与以下相同:
$value = 1;
$var = $value;
if ($var) {
echo "The value is $var";
}
Run Code Online (Sandbox Code Playgroud)
另一个例子:
// Outputs "The value is 1"
$value = 1;
echo "The value is ".$var = $value;
Run Code Online (Sandbox Code Playgroud)
我一直在使用它来缩短我的代码,主要是第一个示例:用于评估第一个变量或表达式,同时将其分配给同一表达式中的另一个变量或表达式。像这样的东西:
if ($status = User::save($data)) {
echo "User saved.";
}
// do something else with $status
Run Code Online (Sandbox Code Playgroud)
这看起来很基本,但我实际上找不到任何关于此的文档,也许我不知道在哪里查找。在看到它多年后,我最近才弄清楚它是如何工作的,我真的很喜欢使用它,但我不想随意使用它。
它使代码更短,也许对某些人来说不太清楚,但绝对减少了重复性。这种方法有什么注意事项吗?这是否完全安全,或者是否存在可能失败或导致意外行为的情况?这似乎不是一种很常见的做法,所以我希望在开始“疯狂”之前找到一个解释。如果有记录,我们将非常感谢指向正确页面的链接。
我知道
var a = [12,23,132,12,3];
var [first, ...rest] = a;
Run Code Online (Sandbox Code Playgroud)
将给予first = 12和rest = [23,132,12,3]
我想做的是将 makerest作为第一个变量。像这样的东西
var a = [12,23,132,12,3];
var [...rest, last] = a;
Run Code Online (Sandbox Code Playgroud)
这应该给我rest = [12,23,132,12]和last = 3
但这是错误的语法,我会收到一个错误,内容是
SyntaxError: Rest element must be last element
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过反转数组然后像这样解构来实现这一点
var a = [12,23,132,12,3];
var [last, secondLast, ...rest] = a.reverse();
Run Code Online (Sandbox Code Playgroud)
这将给我last = 3,secondLast = 12然后rest = [132,23,12]我将再次不得不reverse()休息。
我还可以使用索引并直接访问数组元素。但这并不是我们所希望的。
我的问题是,还有其他方法可以实现我想要做的事情(使用休息运算符吗?)?
javascript arrays destructuring variable-assignment ecmascript-6
C++ 不允许使用=. 但是允许使用 复制结构=,如本链接中所示 -> Copying array in C v/s copying structure in C 。它还没有任何可靠的答案。但请考虑以下代码
#include <iostream>
using namespace std;
struct user {
int a[4];
char c;
};
int main() {
user a{{1,2,3,4}, 'a'}, b{{4,5,6,7}, 'b'};
a = b; //should have given any error or warning but nothing
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码段没有给出任何错误和警告,而且工作正常。为什么?考虑解释两个问题(这个和上面链接的一个)。
begin;
create type public.ltree as (a int, b int);
create table public.parent_tree(parent_id int,l_tree ltree);
insert into public.parent_tree values(1,(2,2)),(2,(1,2)),(3, (1,28));
commit;
Run Code Online (Sandbox Code Playgroud)
尝试复制此答案中的解决方案:
对于复合类型的函数:
CREATE OR REPLACE FUNCTION public.get_parent_ltree
(_parent_id int, tbl_name regclass , OUT _l_tree ltree)
LANGUAGE plpgsql AS
$func$
BEGIN
EXECUTE format('SELECT l_tree FROM %s WHERE parent_id = $1', tbl_name)
INTO _l_tree
USING _parent_id;
END
$func$;
Run Code Online (Sandbox Code Playgroud)
执行的有效查询:
select l_tree from parent_tree where parent_id = 1;
Run Code Online (Sandbox Code Playgroud)
执行函数:
select get_parent_ltree(1,'parent_tree');
select get_parent_ltree(1,'public.parent_tree');
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
begin;
create type …Run Code Online (Sandbox Code Playgroud) 假设我想根据依赖于条件的复杂计算分配一个 const 变量。
如果情况简单,我可以这样做:
const int N = myBool ? 1 : 2;
Run Code Online (Sandbox Code Playgroud)
但它更像是
const int N = myBool ? <lengthy calculation> : <other lengthy calculation>;
Run Code Online (Sandbox Code Playgroud)
我正在做的是这样的,但我想要更干净的东西:
int N_nonconst;
if (myBool) {
N_nonconst = <lengthy calculation>;
}
else {
N_nonconst = <other lengthy calculation>;
}
const int N = N_nonconst;
Run Code Online (Sandbox Code Playgroud)
显然,我也可以这样做:
int possibility1 = <lengthy calculation>;
int possibility2 = <other lengthy calculation>;
const in N = myBool ? possibility1 : possibility2;
Run Code Online (Sandbox Code Playgroud)
但我实际上只想执行这些冗长的计算之一。
如果我要扩展语言,我会考虑做出类似const_deferredAssignment声明的内容:
const_deferredAssignment int N;
if (myBool) …Run Code Online (Sandbox Code Playgroud) C++ 有没有办法模拟这个 python 语法
a,b = b,(a+b)
Run Code Online (Sandbox Code Playgroud)
我知道使用临时变量这是可能的,但我很好奇是否可以不使用临时变量?
setfwhen 与 结合的行为let让我感到困惑。在这里,setf不会更改列表中的第二个值。
(defun to-temp-field (lst)
(let ((old-value (nth 2 lst))
(new-value 'hello))
(progn
(setf old-value new-value)
lst)))
Run Code Online (Sandbox Code Playgroud)
但如果我不这样做let,它会改变:
(defun to-temp-field (lst)
(let ((new-value 'hello))
(progn
(setf (nth 2 lst) new-value)
lst)))
Run Code Online (Sandbox Code Playgroud)
是什么导致了这种行为?
最近我离开学校几天,想在C++中做一个处理内存地址的小程序实验.
我想看看,如果当前正在运行的程序(让它称之为程序A)创建了一个指向堆中int对象的指针,可以被另一个程序看到并进行修改(程序B).
所以对于程序A,这是我的基本代码:
// Program A
#include <iostream>
using namespace std;
int main()
{
// Pointer to an int object in the heap
int *pint = new int(15);
// Display the address of the memory in heap
cout << pint << endl;
// Display the value stored in that address
cout << *pint << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
计划A的产出:
0x641030
15
Run Code Online (Sandbox Code Playgroud)
对于程序B,我查看了如何通过以下链接分配特定的内存地址:http: //www.devx.com/tips/Tip/14104
计划B的代码是:
// Program B
#include <iostream>
using namespace std;
int main()
{
// assign address …Run Code Online (Sandbox Code Playgroud) int a = 1, b;
if(a > 0) b = 1;
if(a <= 0) b = 2;
System.out.println(b);
Run Code Online (Sandbox Code Playgroud)
如果我运行这个,我收到:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The local variable b may not have been initialized at Broom.main(Broom.java:9)
我知道局部变量没有初始化,你有责任这样做,但在这种情况下,第一个if不初始化变量?
java variables compiler-errors initialization variable-assignment
我想知道第8行的内容是什么.
# Iterators
class Fibs:
def __init__(self):
self.a = 0
self.b = 1
def next(self):
self.a, self.b = self.b, self.a+self.b # <--- here
return self.a
def __iter__(self):
return self
fibs = Fibs()
for f in fibs:
if f > 1000:
print f
break
Run Code Online (Sandbox Code Playgroud)
程序的其余部分我真的不需要太多解释.我不确定是什么分配给了什么.
c++ ×4
variables ×3
arrays ×2
python ×2
c++11 ×1
common-lisp ×1
constants ×1
ecmascript-6 ×1
expression ×1
iterator ×1
java ×1
javascript ×1
let ×1
memory ×1
php ×1
plpgsql ×1
postgresql ×1
reference ×1
rowtype ×1
struct ×1
swap ×1