- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Overloaded nested functions
Tue, 2009-02-24, 23:50
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,
Mon, 2009-03-02, 21:47
#2
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] = { ... }
}
...
}
Mon, 2009-03-02, 23:07
#3
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,
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