Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:36:57+00:00 2026-05-28T19:36:57+00:00

Edit ok, great feedback here, got me pointed in the right direction. Use case

  • 0

Edit
ok, great feedback here, got me pointed in the right direction. Use case for invoking anonymous closure is in Scalatra routing layer. I have a bunch of routes that are grouped together under various types, in this example, requests common to teams:

class Router {
  type TeamType <: _Controller with _Team

  get("""(schedules|rosters|teamresults|teamstats)/\d{8}""".r) {
    val clazz :: date = captures

    val obj = (clazz match {
      case "schedules" => new RosterController
      case "rosters" => new ScheduleController
    }).asInstanceOf[TeamType]

    obj.show(date)
  }
}

By wrapping the match expression in a a self-invoked anonymous closure, we avoid tacking on “FooController.asInstanceOf[TeamType]” to each matched case, and instead do the type cast on returned instance, maintaining immutability in the process (i.e. could not “val obj = clazz match {…}” followed by type cast as obj has already been val’d)

I believe that this is as short-form as one can get when creating object instances based on string class name. Of course, saying that, there is likely an FP approach that does the job with even greater concision…

Anyway cool stuff, was missing anonymous closures from Groovy, and now I discover Scala has that covered as well 😉

Original
Not sure how to pull this off in Scala. In Groovy you can both define and invoke an anonymous closure like so:

{String s-> println(s) }("hello")

What is the equivalent in Scala? Also, rather than returning Unit, how would one specify a return type?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:36:59+00:00Added an answer on May 28, 2026 at 7:36 pm
    ((s : String) => println(s))("hello")
    

    As for the return type, just let Scala infer it.

    scala> ((x : Int) => x < 4)(3)
    res0: Boolean = true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use edit-in-place plugin: http://arashkarimzadeh.com/jquery/7-editable-jquery-plugin.html I't works Great! I just need something to check
I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
Edit: Great points all around, dedicated templating language is obviously the way to go.
Railscasts did a great tutorial on how to do multiple edit from a selection.
http://stackoverflow.com should become <a href=http://stackoverflow.com>http://stackoverflow.com</a> EDIT: It would be great if anchor tags from
Edit: The code here still has some bugs in it, and it could do
EDIT: This method actually works great and I asked it then found the solution
Edit: As always, great answer in under 5 minutes :) Turns out if I
I am using this plugin and it works great but i cant edit the
Edit: This question was written in 2008, which was like 3 internet ages ago.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.