- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
"Error occured in an application involving default arguments."
Wed, 2010-05-19, 06:01
Hi
I've got a situation where I have the following:
def getDocuments( hSig:Int, minLength: Option[Int] = None, maxLength: Option[ Int ] = None): List[ Document ] =
.....
and
def getDocuments( hSig:String, minLength: Option[Int] = None, maxLength: Option[ Int ] = None): List[ Document ] =
.....
When compiling I get an error:
[ERROR] /Users/faulkner/work/devel/dupeserver/src/dupeserver/DupeDetection.scala:43: error: ambiguous reference to overloaded definition,
[INFO] both method getDocuments$default$2 in object DAO of type => Option[Int]
[INFO] and method getDocuments$default$2 in object DAO of type => Option[Int]
[INFO] match expected type Option[Int]
[INFO] var docList = DAO.getDocuments( hSigInt )
Now given that the first args in each method are different, I thought the signatures for each method would be distinct and not conflicting with anything.
If I rename one of the methods to something else, it compiles fine.
Can anyone explain what I'm doing wrong?
Cheers,
Ken
Wed, 2010-05-19, 08:07
#2
Re: "Error occured in an application involving default argumen
Hi,
when you have two overloaded methods with defaults you usually getthe correct error message:
object t { def get(x: Int, y: Option[Int] = None) = 0 | def get(x: String, y: Option[Int] = None) = 1 }
<console>:5: error: in object t, multiple overloaded alternatives of method get define default arguments. object t { def get(x: Int, y: Option[Int] = None) = 0 ^
However, if there is also an application expression, the "ambiguous reference" error will be issued first:
scala> object t { def get(x: Int, y: Option[Int] = None) = 0 | def get(x: String, y: Option[Int] = None) = 1 | get(1) }<console>:7: error: ambiguous reference to overloaded definition, both method get$default$2 in object t of type => Option[Int]and method get$default$2 in object t of type => Option[Int] match expected type Option[Int] get(1) } ^
I'm not sure if we can do something about this; the problem is that the twosynthetic "get$default$2" methods are generated early, before checking that only one alternative has a default, and for some reason the application getstype-checked before the two method definitions.
Lukas
On Wed, May 19, 2010 at 07:01, Ken Faulkner <ken.faulkner@gmail.com> wrote:
when you have two overloaded methods with defaults you usually getthe correct error message:
object t { def get(x: Int, y: Option[Int] = None) = 0 | def get(x: String, y: Option[Int] = None) = 1 }
<console>:5: error: in object t, multiple overloaded alternatives of method get define default arguments. object t { def get(x: Int, y: Option[Int] = None) = 0 ^
However, if there is also an application expression, the "ambiguous reference" error will be issued first:
scala> object t { def get(x: Int, y: Option[Int] = None) = 0 | def get(x: String, y: Option[Int] = None) = 1 | get(1) }<console>:7: error: ambiguous reference to overloaded definition, both method get$default$2 in object t of type => Option[Int]and method get$default$2 in object t of type => Option[Int] match expected type Option[Int] get(1) } ^
I'm not sure if we can do something about this; the problem is that the twosynthetic "get$default$2" methods are generated early, before checking that only one alternative has a default, and for some reason the application getstype-checked before the two method definitions.
Lukas
On Wed, May 19, 2010 at 07:01, Ken Faulkner <ken.faulkner@gmail.com> wrote:
Hi
I've got a situation where I have the following:
def getDocuments( hSig:Int, minLength: Option[Int] = None, maxLength: Option[ Int ] = None): List[ Document ] =
.....
and
def getDocuments( hSig:String, minLength: Option[Int] = None, maxLength: Option[ Int ] = None): List[ Document ] =
.....
When compiling I get an error:
[ERROR] /Users/faulkner/work/devel/dupeserver/src/dupeserver/DupeDetection.scala:43: error: ambiguous reference to overloaded definition,
[INFO] both method getDocuments$default$2 in object DAO of type => Option[Int]
[INFO] and method getDocuments$default$2 in object DAO of type => Option[Int]
[INFO] match expected type Option[Int]
[INFO] var docList = DAO.getDocuments( hSigInt )
Now given that the first args in each method are different, I thought the signatures for each method would be distinct and not conflicting with anything.
If I rename one of the methods to something else, it compiles fine.
Can anyone explain what I'm doing wrong?
Cheers,
Ken
Hi Ken,
According to the named args SID, "If there are multiple overloaded alternatives of a method, at most one is allowed to specify default arguments." One could argue whether the error you are seeing is the best one, but it would appear that this situation is outlawed.
regards,
Tony
On 19/05/2010, at 3:01 PM, Ken Faulkner wrote:
> Hi
>
> I've got a situation where I have the following:
>
> def getDocuments( hSig:Int, minLength: Option[Int] = None, maxLength: Option[ Int ] = None): List[ Document ] =
> .....
>
> and
>
> def getDocuments( hSig:String, minLength: Option[Int] = None, maxLength: Option[ Int ] = None): List[ Document ] =
> .....
>
>
> When compiling I get an error:
>
> [ERROR] /Users/faulkner/work/devel/dupeserver/src/dupeserver/DupeDetection.scala:43: error: ambiguous reference to overloaded definition,
> [INFO] both method getDocuments$default$2 in object DAO of type => Option[Int]
> [INFO] and method getDocuments$default$2 in object DAO of type => Option[Int]
> [INFO] match expected type Option[Int]
> [INFO] var docList = DAO.getDocuments( hSigInt )
>
>
>
> Now given that the first args in each method are different, I thought the signatures for each method would be distinct and not conflicting with anything.
> If I rename one of the methods to something else, it compiles fine.
>
> Can anyone explain what I'm doing wrong?
>
> Cheers,
>
> Ken
>