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

Is there shorthand for 'object Lit { def apply(lit:String) = new Literal(lit) } '?

2 replies
Kenneth McDonald
Joined: 2009-01-11,
User offline. Last seen 42 years 45 weeks ago.

Given that one cannot have top-level functions in a Scala program (as
opposed them being allowed in a script), I would expect that an idiom
like:

object Lit { def apply(lit:String) = new Literal(lit) }

to achieve the effect of defining a top-level function called "Lit",
would be common. Is there a shorthand for this by any chance? Scala
has so many neat bits of shorthand, I don't want to miss one :-)

Thanks,
Ken

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there shorthand for 'object Lit { def apply(lit:String)

Case class Lit(name: String)

On Jan 16, 2009 5:24 PM, "Kenneth McDonald" <kenneth [dot] m [dot] mcdonald [at] sbcglobal [dot] net> wrote:

Given that one cannot have top-level functions in a Scala program (as opposed them being allowed in a script), I would expect that an idiom like:

object Lit { def apply(lit:String) = new Literal(lit) }

to achieve the effect of defining a top-level function called "Lit", would be common. Is there a shorthand for this by any chance? Scala has so many neat bits of shorthand, I don't want to miss one :-)

Thanks,
Ken

Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there shorthand for 'object Lit { def apply(lit:String)

Why not just have:

object LiteralFunctions {
def lit(lit:String) = new Literal(lit)
}

then use

import LiteralFunctions._

then the lit function and any others you define will be available.
I.e. use the object as a module for top level functions.

On 1/17/09, Kenneth McDonald wrote:
> Given that one cannot have top-level functions in a Scala program (as
> opposed them being allowed in a script), I would expect that an idiom like:
>
> object Lit { def apply(lit:String) = new Literal(lit) }
>
> to achieve the effect of defining a top-level function called "Lit", would
> be common. Is there a shorthand for this by any chance? Scala has so many
> neat bits of shorthand, I don't want to miss one :-)
>
> Thanks,
> Ken
>

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