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

Overloaded nested functions

3 replies
normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.

He,

I have a function and want to define two more functions within this body:

def foo(n: Int): Option[String] = {
def bar(u: URI): Option[String] = { ... }
def bar(l: Long, u: URI): Option[String] = { ... }
...
}

But the compiler complains ``bar'' to be defined twice. So isn't this possible at all?

Cheers,

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Overloaded nested functions

On Tue, Feb 24, 2009 at 11:50 PM, Normen Müller
wrote:
> He,
>
> I have a function and want to define two more functions within this body:
>
> def foo(n: Int): Option[String] = {
>  def bar(u: URI): Option[String] = { ... }
>  def bar(l: Long, u: URI): Option[String] = { ... }
>  ...
> }
> But the compiler complains ``bar'' to be defined twice.  So isn't this
> possible at all?
>
No, only class members are allowed to be overloaded.

Cheers

geoff
Joined: 2008-08-20,
User offline. Last seen 1 year 25 weeks ago.
Re: Overloaded nested functions

On Tue, Feb 24, 2009 at 11:50:29PM +0100, Normen M?ller said
> He,
>
> I have a function and want to define two more functions within this
> body:
>
> def foo(n: Int): Option[String] = {
> def bar(u: URI): Option[String] = { ... }
> def bar(l: Long, u: URI): Option[String] = { ... }
> ...
> }
>
> But the compiler complains ``bar'' to be defined twice. So isn't this
> possible at all?

You can do this:

def foo(n: Int): Option[String] = {
object bar {
def apply(u: URI): Option[String] = { ... }
def apply(l: Long, u: URI): Option[String] = { ... }
}
...
}

normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.
Re: Overloaded nested functions

Geoff Reedy wrote:
> You can do this:
>
> def foo(n: Int): Option[String] = {
> object bar {
> def apply(u: URI): Option[String] = { ... }
> def apply(l: Long, u: URI): Option[String] = { ... }
> }
> ...
> }

Ah, also nice ;) Thanks!

Cheers,

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