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

top-level functions in a package

3 replies
Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
I am just starting to learn Scala. It doesn't seem to let me put top-level functions (i.e., regular functions that are not methods of a class) in a package. Am I missing something? I can't find anything about it in the Odersky book. Thanks.

Russ P.

--
http://RussP.us
James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: top-level functions in a package
One way to look at

object Foo {
  def f(x:Int) = ...
}

Is that f is a top level function in the Foo module.

On Sat, Jan 31, 2009 at 8:41 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
I am just starting to learn Scala. It doesn't seem to let me put top-level functions (i.e., regular functions that are not methods of a class) in a package. Am I missing something? I can't find anything about it in the Odersky book. Thanks.

Russ P.

--
http://RussP.us

Jens Alfke
Joined: 2009-01-30,
User offline. Last seen 42 years 45 weeks ago.
Re: top-level functions in a package

On Jan 31, 2009, at 8:41 PM, Russ Paielli wrote:

> I am just starting to learn Scala. It doesn't seem to let me put top-
> level functions (i.e., regular functions that are not methods of a
> class) in a package.

Right. It inherits this from Java — there are no bare functions, only
methods of classes.
If you need a standalone function, use the 'object' keyword to declare
a singleton object and make it a method of that.

—Jens

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: top-level functions in a package
or an object extending FunctionN and then implement the apply() method and voilá?

On Sun, Feb 1, 2009 at 6:04 AM, Jens Alfke <jens@mooseyard.com> wrote:

On Jan 31, 2009, at 8:41 PM, Russ Paielli wrote:

I am just starting to learn Scala. It doesn't seem to let me put top-level functions (i.e., regular functions that are not methods of a class) in a package.

Right. It inherits this from Java — there are no bare functions, only methods of classes.
If you need a standalone function, use the 'object' keyword to declare a singleton object and make it a method of that.

—Jens




--
Viktor Klang
Senior Systems Analyst

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