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

  • SEARCH
  • Home
  • 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 7609461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:10:38+00:00 2026-05-31T01:10:38+00:00

I have a method, with have a lot of implicit parameters: def hello(message:String)(implicit a:A,b:B,c:C,

  • 0

I have a method, with have a lot of implicit parameters:

def hello(message:String)(implicit a:A,b:B,c:C, ..., user: User) = {...}

Now consider such a class:

object Users extends Controller {
  implicit a: A = ...
  implicit b: B = ...
  ...

  def index(id:String) = Action {
     User.findById(id) match {
       case Some(user) => {
          implicit val _user = user
          hello("implicit")
       }
       case _ => BadRequest
     }
  }
}

You can see this line in the above sample:

implicit val _user = user

It exists just to make the object user as an implicit object. Otherwise, I have to call hello as:

hello("implicit")(a,b,c,... user)

I’m thinking if there is any way to improve the code, e.g. we don’t need to define that _user variable but make the user is implicit.

  • 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-31T01:10:39+00:00Added an answer on May 31, 2026 at 1:10 am

    Yes, there is a way to eliminate _user variable while making user implicit:

    def index(id:String) = Action {
      User.findById(id) map (implicit user => hello("implicit")) getOrElse BadRequest
    }
    

    UPDATE: Addressing your question about many cases in the comments below.

    It all depends what value type is returned by User.findById. If it’s Option[User] but you want to match on specific users (assuming User is a case class), then the original solution still applies:

    def index(id:String) = Action {
      User.findById(id) map { implicit user =>
        user match {
          case User("bob") => hello("Bob")
          case User("alice") => hello("Alice")
          case User("john") => hello("John")
          case _ => hello("Other user")
        }
      } getOrElse BadRequest
    

    Or you can match on anything else if you want, as long as User.findById is String => Option[User]

    If, on the other hand, User.findById is String => User then you can simply define a helper object like:

    object withUser {
      def apply[A](user: User)(block: User => A) = block(user)
    }
    

    And use it as follows (again assuming User is a case class):

    def index(id: String) = Action {
      withUser(User findById id) { implicit user =>
        user match {
          case User("bob") => hello("Bob")
          case User("alice") => hello("Alice")
          case User("john") => hello("John")
          case _ => BadRequest
        }
      }
    }
    

    or matching on some other value, say an Int:

    def index(id: String, level: Int) = Action {
      withUser(User findById id) { implicit user =>
        level match {
          case 1 => hello("Number One")
          case 2 => hello("Number Two")
          case 3 => hello("Number Three")
          case _ => BadRequest
        }
      }
    }
    

    I hope this covers all the scenarios you may have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method that returns lot of data, should I use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) for
Have a lot of unnecessary results using contains() method in my query. Don't tell
I have method in a class that I need to make sure is only
I have class method that returns a list of employees that I can iterate
I have a method that where I want to redirect the user back to
I have edited and simplified this question a lot. If I have this method
I have that method that counts files in a certain folder: private void countfiles(string
I have a method in a class for which they are a few different
I have a method register() that takes a lot of time to complete. So

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.