Scala - Currying and default arguments?

The type inference engine gives to partial the type of what comes next; i.e. , the eta expansion test(1) You can see e.g. In the REPL that partial has type (Int, Int) => Unit whereas test has type (a: Int)(b: Int,c: Int)Unit The result of the eta expansion is a Function object, which does not carry any argument names with it (as it is possible to define Function with anonymous parameters) To fix this, you have to define partial as follows: def partial(b: Int = 2, c: Int = 3) = test(1)(b,c) Maybe you'll want to factor out the default values where both test and partial can reach them to make sure they stay equal. But I know of no trick to avoid repeating the names of the parameters without introducing extra overhead like creating of new objects, etc.

The type inference engine gives to partial the type of what comes next; i.e. , the eta expansion test(1) _. You can see e.g.In the REPL that partial has type (Int, Int) => Unit, whereas test has type (a: Int)(b: Int,c: Int)Unit.

The result of the eta expansion is a Function object, which does not carry any argument names with it (as it is possible to define Function with anonymous parameters). To fix this, you have to define partial as follows: def partial(b: Int = 2, c: Int = 3) = test(1)(b,c) Maybe you'll want to factor out the default values where both test and partial can reach them to make sure they stay equal. But I know of no trick to avoid repeating the names of the parameters without introducing extra overhead like creating of new objects, etc.

The Function object does carry argument names but those are the argument names defined in Function2 You can actually invoke the example given by @Desperate this way: partial(v1 = 2, v2 = 3). – Moritz Apr 15 at 12:21 I figured it had to do with the compiler not creating new classes for functions like this but using "generic" Function2 instead. Otherwise it could have stored all the info it needs in annotations.

The whole point was to avoid repeating argument names/list, though :( – Learner Apr 15 at 12:25 @Moritz, I realize that, but that's not useful at all when one wants to preserve proper names. This truly is a deficiency if not a defect of the language. I never understood why is Scala trying to reuse pre-created Function classes instead of generating them as needed.

I know that would generate more classes, but it would be both the correct thing to do and useful in the end. On a side note, I'll try generating a new Function-like object of my own, with 'apply' method in it... – Learner Apr 15 at 12:50 @Desperate I did not say it was useful - it could even be harmful if your parameters happen to have the same names in a different order. Scala actually does generate a subclass of FunctionN cases like this but returning a specialized subtype here would make overloading quite tricky.

– Moritz Apr 15 at 13:18 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 + ", " + be + ", " + 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... – Learner Apr 15 at 13:21.

Following up on your comment, here's a more compact way to write it: def test(a: Int) = new { def apply(b: Int = 2, c: Int = 3) { println(a + ", " + be + ", " + c) } } This is a bit more compact than your proposal, but is less efficient, as any call to the inner apply will occur through reflection, as with structural types. Actually, the return type of test is a structural type: java.lang. Object{def apply(b: Int,c: Int): Unit; def apply$default$1: Int @scala.annotation.unchecked.

UncheckedVariance; def apply$default$2: Int @scala.annotation.unchecked. UncheckedVariance}.

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 + ", " + be + ", " + 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...

Maybe you'll want to factor out the default values where both test and partial can reach them to make sure they stay equal. But I know of no trick to avoid repeating the names of the parameters without introducing extra overhead like creating of new objects, etc.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions