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

Method overload by anonymous function parameter

1 reply
gnufied
Joined: 2008-08-19,
User offline. Last seen 3 years 18 weeks ago.

I know following code won't work:

class Foo {
def hello(myName: String) = {
println("My Name is : " + myName)
}

def hello(myName: String)(func: Bar => Unit) = {
println("My name is " + myName)
func(new Bar(myName + " O Hara" ))
}
}

class Bar(val myName: String) {
println("Constructing the class")
}

object App {
def main(args : Array[String]) : Unit = {
val a = new Foo()
a.hello("Scarlet")

a.hello("Violet"){ tbar => {
println(tbar.myName)
}}
}
}

But, what alternatives one have, if I want to overload a method
definition by function argument. On #scala, folks suggested implicits
can be used and truly enough this compiles:

class Foo {
def hello(myName: String)(implicit func: Bar => Unit) = {
println("My name is " + myName)
func(new Bar(myName + " O Hara" )) //line#4
}
}

case class Bar(val myName: String) {
println("Constructing the class")
}

object App {
def main(args : Array[String]) : Unit = {
val a = new Foo()
a.hello("Scarlet")

a.hello("Violet"){ tbar => {
println(tbar.myName)
}}
}
}

But with the catch that, compiler is still supplying func if
definition is implicit and I am not invoking hello with any funciton
argument (or there is no matching implicit in scope) and
hence object of Bar gets created in first call as well. I wouldn't want that.

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Method overload by anonymous function parameter
Another option is to not curry your overloaded hello.  The price is a slightly more awkward use of parens.

  class Foo {
 def hello(myName: String) = {
   println("My Name is : " + myName)
 }

 def hello(myName: String, func: Bar => Unit) = {
   println("My name is " + myName)
   func(new Bar(myName + " O Hara" ))
 }
}

class Bar(val myName: String)  {
 println("Constructing the class")
}

object App {
 def main(args : Array[String]) : Unit = {
   val a = new Foo()
   a.hello("Scarlet")

   a.hello("Violet", {tbar =>
     println(tbar.myName)
   })
 }
 }

On Sat, Dec 27, 2008 at 10:55 PM, hemant <gethemant [at] gmail [dot] com> wrote:
I know following code won't work:

class Foo {
 def hello(myName: String) = {
   println("My Name is : " + myName)
 }

 def hello(myName: String)(func: Bar => Unit) = {
   println("My name is " + myName)
   func(new Bar(myName + " O Hara" ))
 }
}

class Bar(val myName: String)  {
 println("Constructing the class")
}

object App {
 def main(args : Array[String]) : Unit = {
   val a = new Foo()
   a.hello("Scarlet")

   a.hello("Violet"){ tbar => {
     println(tbar.myName)
   }}
 }
}

But, what alternatives one have, if I want to overload a method
definition by function argument. On #scala, folks suggested implicits
can be used and truly enough this compiles:

class Foo {
 def hello(myName: String)(implicit func: Bar => Unit) = {
   println("My name is " + myName)
   func(new Bar(myName + " O Hara" )) //line#4
 }
}

case class Bar(val myName: String)  {
 println("Constructing the class")
}

object App {
 def main(args : Array[String]) : Unit = {
   val a = new Foo()
   a.hello("Scarlet")

   a.hello("Violet"){ tbar => {
     println(tbar.myName)
   }}
 }
}

But with the catch that, compiler is still supplying func if
definition is implicit and I am not invoking hello with any funciton
argument (or there is no matching implicit in scope) and
hence object of Bar gets created in first call as well. I wouldn't want that.



--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org

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