Overloaded method/function name annoyances

View: New views
2 Messages — Rating Filter:   Alert me  

Overloaded method/function name annoyances

by toivo :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Hi,


object Annoy1 {

  def paymentInv( isa: double, amount: double, ex: double) = isa * amount/1000 * ex
 
  def paymentInv = paymentInv( 24, 7000, 0.9)
}

   Error:  overloaded method paymentInv needs result type


object Annoy1 {

  def paymentInv( isa: double, amount: double, ex: double) = isa * amount/1000 * ex
 
  val paymentInv = paymentInv( 24, 7000, 0.9)
}

   Error: recursive value paymentInv needs type

Why result type is required?
And this is not recursive – I am calling different method/function with same name but signature is different.


package von.lab

object calcEngine {
   
  def paymentInv( isa: double, amount: double, ex: double) = isa * amount/1000 * ex
}

object Annoy2 {

  import von.lab.calcEngine._
 
  val paymentInv: double = paymentInv( 24, 7000, 0.9)
}

   Error: Annoy2.this.paymentInv of type double does not take parameters

Overloading (using same method name) works only inside same object/class?
Different signature is not enough for compiler?

Am I missing something?

toivo

Re: Overloaded method/function name annoyances

by Martin Odersky :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On Jan 10, 2008 1:19 PM, toivo <tom.tad@...> wrote:

>
> Hi,
>
>
> object Annoy1 {
>
>   def paymentInv( isa: double, amount: double, ex: double) = isa *
> amount/1000 * ex
>
>   def paymentInv = paymentInv( 24, 7000, 0.9)
> }
>
>    Error:  overloaded method paymentInv needs result type
>
>
> object Annoy1 {
>
>   def paymentInv( isa: double, amount: double, ex: double) = isa *
> amount/1000 * ex
>
>   val paymentInv = paymentInv( 24, 7000, 0.9)
> }
>
>    Error: recursive value paymentInv needs type
>
> Why result type is required?
> And this is not recursive – I am calling different method/function with same
> name but signature is different.
>
Overloaded methods need an explicit result type because
the result type is one of the things that's used to determine
whether a method alternative is applicable or not. There's only so
much one can do. If someone specifies a complete solution to me (in
the language of the JLS) which is not too complicated and which is
implementable, I'd be happy to implement it.

The ``recursive value'' message is a related, but different story --
here, the compiler found that there exist two paymentInv and tried to
determine the correct alternative. In doing that it needed the type of
the value paymentInv, which gave the recursive value error. (and, just
to respond to a possible objection -- the
value paymentInv cannot be ruled out out of hand because
it might be a function value that is applicable to the three arguments).

>
> Overloading (using same method name) works only inside same object/class?

Indeed. The compiler will first look up the members based on the rules
of nesting and import precedence. Then, if there are several eligible
members, overloading resolution is applied.

Cheers

 -- Martin