问题描述
我有,我想申请部分与柯里用两个参数列表的功能。第二个参数列表包含参数,所有有默认值(但不是隐含的)。事情是这样的:
I have a function with two parameter lists that I am trying to partially apply and use with currying. The second parameter list contains arguments that all have default values (but not implicit). Something like this:
def test(a: Int)(b: Int = 2, c: Int = 3) { println(a + ", " + b + ", " + c); }
现在,以下是所有罚款:
Now, the following is all fine:
test(1)(2, 3);
test(1)(2);
test(1)(c=3);
test(1)();
现在,如果我定义:
def partial = test(1) _;
那么下面可以做:
Then the following can be done:
partial(2, 3);
有人能解释我为什么不能省略一些/部分所有参数如下:
Can someone explain why I can't omit some/all arguments in 'partial' as follows:
partial(2);
partial(c=3);
partial();
不应该写部分的行为本质上是方式测试(1)一样吗?有人可以帮我想出一个办法来实现这一目标?
Shouldn't writing "partial" behave essentially the same way as "test(1)"? Can someone please help me figure out a way to achieve this?
请帮忙,我很绝望!
修改 - 既然不能在24小时内回答我的问题,我会在这里发表我自己的答案:
EDIT - Since I can't answer my own question within 24 hours, I'll post my own answer here:
这是我可以做自己,所以目前最好的:
This is the best I could do myself so far:
class Test2(val a: Int) {
def apply(b: Int = 2, c: Int = 3) { println(a + ", " + b + ", " + c); }
}
def test2(a: Int) = new Test2(a);
def partial2 = test2(1); // Note no underscore
test2(1)(2, 3);
test2(1)(2);
test2(1)(c=3);
test2(1)();
partial2(2, 3)
partial2(2);
partial2(c=3);
partial2();
此方式,它的工作原理...
This way it works...
推荐答案
这是我可以做自己,所以目前最好的:
This is the best I could do myself so far:
class Test2(val a: Int) {
def apply(b: Int = 2, c: Int = 3) { println(a + ", " + b + ", " + c); }
}
def test2(a: Int) = new Test2(a);
def partial2 = test2(1); // Note no underscore
test2(1)(2, 3);
test2(1)(2);
test2(1)(c=3);
test2(1)();
partial2(2, 3)
partial2(2);
partial2(c=3);
partial2();
此方式,它的工作原理...
This way it works...
这篇关于斯卡拉 - 柯里和默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!