This page is no longer maintained — Please continue to the home page at www.scala-lang.org

SID#1 (named arguments) and Scala implementation diverge

3 replies
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.

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

rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: SID#1 (named arguments) and Scala implementation diverge
Hello Francois,

sorry for the long delay. Answers inline.


On Sun, Oct 25, 2009 at 13:01, Francois Armand <fanf42@gmail.com> wrote:
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<-----------------------------------------------


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")
         ^



 


*** 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<-----------------------------------------------



same thing here, I'm afraid...

 



*** 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<-----------------------------------------------


right, thanks.
 



*** 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]()
<console>:6: error: type mismatch;
 found   : java.lang.String
 required: Int
Error occured in an application involving default arguments.
      g[Int]()
       ^
8<-----------------------------------------------


I just shortened the error message a bit to make it fit on one line.



Hope it helps,



Yes, thanks for the feedback!
Lukas


fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
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

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland