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

self referencing function

3 replies
Lars Gersmann
Joined: 2009-01-05,
User offline. Last seen 42 years 45 weeks ago.
hello,

actually i want to define a function returning a function ... its probably simple but i dont get it.

from java point of view i would write something like

trait ICommand {
  def apply : ICommand
}

is it possible to declare that as a single function ?

many regards,

lars
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: self referencing function

On Mon, Jan 05, 2009 at 11:35:02PM +0100, Lars Gersmann wrote:
> actually i want to define a function returning a function ... its probably
> simple but i dont get it.
>
> from java point of view i would write something like
>
> trait ICommand {
> def apply : ICommand
> }

scala> trait SelfRef extends Function0[SelfRef] { def apply(): SelfRef = this }
defined trait SelfRef

scala> new AnyRef with SelfRef
res0: java.lang.Object with SelfRef =

scala> res0()
res1: SelfRef =

scala> res1()
res2: SelfRef =

> is it possible to declare that as a single function ?

Define "single function". I don't think you can do this with anonymous functions.

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: self referencing function
Unfortunately, this isn't allowed

type ICommand = (() => ICommand)

But this is

 trait ICommand extends (() => ICommand)

On Mon, Jan 5, 2009 at 2:35 PM, Lars Gersmann <lars [dot] gersmann [at] gmail [dot] com> wrote:
hello,

actually i want to define a function returning a function ... its probably simple but i dont get it.

from java point of view i would write something like

trait ICommand {
  def apply : ICommand
}

is it possible to declare that as a single function ?

many regards,

lars

ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: self referencing function

James Iry wrote:
> Unfortunately, this isn't allowed
>
> type ICommand = (() => ICommand)

type ICommand = () => T forSome { type T <: () => T }

trait A extends (() => A)
def f(a: A): ICommand = a

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