我正在尝试为选项参数定义具有以下行为的构造函数:
class Test {
constructor({
someOption = 'foo',
otherOption = 'bar',
aSeedOfSomeSort = Math.random(),
// ...
}) {
console.log(
someOption, otherOption, aSeedOfSomeSort
);
}
}
new Test({someOption: 'lol'});
new Test({otherOption: 'derp'});
new Test({aSeedOfSomeSort: 0.5});
new Test({});
new Test(); // here's the problemRun Code Online (Sandbox Code Playgroud)
这很好用,但是,我希望构造函数能够工作,以便使用所有默认参数,我不必传递空对象。我不关心对象本身是否在参数中命名,但在构造函数的上下文中,我想要一种干净的方法来直接访问所有选项,而无需命名空间或使用with.
这可能吗?
打开-Wextra标志gcc似乎具有禁止structs部分初始化的效果。例如:
// main.c
#include <pthread.h>
typedef struct S {
int i;
pid_t pid;
} S;
int main( int argc, char* argv[] ) {
(void)argc;
(void)argv;
S s = { 42 };
(void)s;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ gcc --version && gcc -Wall -Wextra -pedantic -Werror ./main.c
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for …Run Code Online (Sandbox Code Playgroud) 我有高阶函数:
\ndef\xc2\xa0calculateFusionRiskContract(postCalc:\xc2\xa0DataFrame\xc2\xa0=>\xc2\xa0DataFrame) \nRun Code Online (Sandbox Code Playgroud)\n如何设置仅返回 postCalc 参数的 postCalc 参数的默认值?没有任何计算?
\ndef\xc2\xa0calculateFusionRiskContract(postCalc:\xc2\xa0DataFrame\xc2\xa0=>\xc2\xa0DataFrame = ???) \nRun Code Online (Sandbox Code Playgroud)\n Integer i = null;
int j = i;
System.out.println(j);
Run Code Online (Sandbox Code Playgroud)
为什么它抛出NullPointerException并且不打印jas的值0?
函数中的默认值到底是如何工作的?我的问题与这个例子有关:
int func(int number, std::string name = "None", int anotherNumber);
..
int func(int number, std::string name, int anotherNumber){
...
}
func(1, 2);
^Error, name is NULL yet we've defined a default value?
Run Code Online (Sandbox Code Playgroud)
编译器给出一个错误,抱怨参数为 NULL 并且不应该为 NULL。但我已经为其定义了默认值。
为什么是这样?
c ×1
c++ ×1
constructor ×1
ecmascript-6 ×1
gcc ×1
java ×1
javascript ×1
pid ×1
posix ×1
scala ×1