- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
SID#1 (named arguments) and Scala implementation diverge
Sun, 2009-10-25, 13:01
Hello,
There is some divergences between the example in "Named and Default
Arguments in Scala 2.8" SID ( http://www.scala-lang.org/sid/1 ) and what
Scala 2.8 of 21 October 2009 process.
I believe that the first two are important since they seem to reveal a
modification in the Overloading resolution. The other are just typos and
error reporting alteration. Let me know if I have to open tickets for them.
All tests are done with:
8<-----------------------------------------------
% scala
Welcome to Scala version 2.8.0.r19180-b20091021023451 (Java HotSpot(TM)
Server VM, Java 1.6.0_16).
8<-----------------------------------------------
*** 1 ***
IMPORTANT Page 3, Overloading Resolution : the last example doesn't
raise an error:
8<-----------------------------------------------
scala> def f(a: Int, b: String)= { println("#1") }
f: (a: Int,b: String)Unit
scala> def f(b: Object, a: Int)= { println("#2") }
f: (b: java.lang.Object,a: Int)Unit
scala> f(a = 1, b = "someString") // "error: ambiguous reference to
// overloaded definition"
#2
8<-----------------------------------------------
*** 2 ***
IMPORTANT, page 5, Overloading Resolution : the actually selected method
is not the one reported:
8<-----------------------------------------------
scala> def f(a: Object) = { println("#1") }
f: (a: java.lang.Object)Unit
scala> def f(a: String, b: Int = 1) = { println("#2") }
f: (a: String,b: Int)Unit
scala> f("str") // both are applicable, #1 is selected
#2
8<-----------------------------------------------
*** 3 ***
Page 4, in chapter "3 Default arguments", last example with g : typos
("1" and 2 inverted):
8<-----------------------------------------------
scala> def g[T](a: T = 1, b: T = "2") = a
g: [T](a: T,b: T)T
scala> g(a = "1") // OK, returns "2": String
res6: java.lang.String = 1
scala> g(b = 2) // OK, returns 2: Int
res7: Int = 1
scala> g() // OK, returns "2": Any
res8: Any = 1
8<-----------------------------------------------
*** 4 ***
Page 4, 3 Default arguments, last example with g : an other error than
the reported one is found:
8<-----------------------------------------------
scala> def g[T](a: T = 1, b: T = "2") = a
g: [T](a: T,b: T)T
scala> g[Int]()
:6: error: type mismatch;
found : java.lang.String
required: Int
Error occured in an application involving default arguments.
g[Int]()
^
8<-----------------------------------------------
Hope it helps,
--
Francois Armand
http://fanf42.blogspot.com
Mon, 2009-11-09, 17:27
#2
Re: SID#1 (named arguments) and Scala implementation diverge
On 09/11/2009 17:17, Lukas Rytz wrote:
> Hello Francois,
> sorry for the long delay. Answers inline.
No problem ;)
> You fell into a trap of the interpreter. If you want overloaded methods
> you need to put them
OK, glad to see that it's a problem of mine, not of the spec - far
better this way :)
--
Francois Armand
Mon, 2009-11-09, 17:37
#3
Re: SID#1 (named arguments) and Scala implementation diverge
On Mon, Nov 09, 2009 at 05:17:31PM +0100, Lukas Rytz wrote:
> You fell into a trap of the interpreter. If you want overloaded
> methods you need to put them in an object, i.e.
>
> scala> object t { def f(a: Int, b: String)= ...
REPL pro tip: You can also define overloaded methods like this:
scala> def foo(x: Int) = x ; def foo(x: String) = x.toInt
foo: (x: String)Int (x: Int)Int
foo: (x: String)Int (x: Int)Int
sorry for the long delay. Answers inline.
On Sun, Oct 25, 2009 at 13:01, Francois Armand <fanf42@gmail.com> wrote:
You fell into a trap of the interpreter. If you want overloaded methods you need to put them
in an object, i.e.
scala> object t { def f(a: Int, b: String)= { println("#1") }
| def f(b: Object, a: Int)= { println("#2") } }
defined module t
scala> t.f(a = 1, b = "2")
<console>:6: error: ambiguous reference to overloaded definition,
both method f in object t of type (b: java.lang.Object,a: Int)Unit
and method f in object t of type (a: Int,b: String)Unit
match argument types (a: Int,b: java.lang.String)
t.f(a = 1, b = "2")
^
same thing here, I'm afraid...
right, thanks.
I just shortened the error message a bit to make it fit on one line.
Yes, thanks for the feedback!
Lukas