top-level functions in a package

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

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

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 [at] mooseyard [dot] 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

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 [dot] paielli [at] gmail [dot] 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

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