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 7789405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:17:18+00:00 2026-06-01T21:17:18+00:00

I have been trying to get a grasp on how implicit parameters work in

  • 0

I have been trying to get a grasp on how implicit parameters work in Scala. As far as I can tell the implicit parameter resolution goes something like this:

  1. Explicitly passing an object to the method.
  2. implicit definitons defined in scope.
  3. Companion object of the class used as a implicit parameter

However, when I started playing around with this in conjunction lazy vals I got a bit of a supprise. It seems that lazy vals only ever use the last resolution rules. Here is some sample code to illustrate:

class Bar(val name:String)
object Bar { implicit def bar = new Bar("some default bar") }

class Foo {
  lazy val list = initialize
  def initialize(implicit f:Bar) = {
    println("initialize called with Bar: '" + f.name + "' ...")
    List[Int]()
  }
}

trait NonDefaultBar extends Foo {
  implicit def f = new Bar("mixed in implicit bar")
  def mixedInInit = initialize
  lazy val mixedInList = list
}

object Test {
    def test = {
      println("Case 1: with implicitp parameter from companion object")
      val foo1 = new Foo
      foo1.list
      foo1.initialize

      println("Case 2: with mixedin implicit parameter overriding the default one...")
      val foo2 = new Foo with NonDefaultBar 
      foo2.mixedInList

      val foo3 = new Foo with NonDefaultBar 
      foo3.mixedInInit

      println("Case 3: with local implicit parameter overriding the default one...")
      implicit def nonDefaultBar = new Bar("locally scoped implicit bar")
      val foo4 = new Foo 
      foo4.list
      foo4.initialize
    }
}

Calling Test.test gives the following output:

Case 1: with implicitp parameter from companion object 
initialize called with Bar: 'some default bar' ... 
initialize called with Bar: 'some default bar' ... 
Case 2: with mixedin implicit parameter overriding the default one... 
initialize called with Bar: 'some default bar' ... 
initialize called with Bar: 'mixed in implicit bar'... 
Case 3: with local implicit parameter overriding the default one... 
initialize called with Bar: 'some default bar' ... 
initialize called with Bar: 'locally scoped implicit bar' ...

Why does the compiler not catch that there is a implict Bar mixed in when calling mixedInList in Case 2. In Case 3 it also misses the locally defined implicit Bar when accessing the list.

Are there any ways to use implicit parameters with lazy vals that does not use the implicit defined in the companion object?

  • 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-06-01T21:17:21+00:00Added an answer on June 1, 2026 at 9:17 pm

    That is because there is no other implicit Bar, when the compiler compiles the Foo class. The decompiled code in Java look like this:

    public class Foo
      implements ScalaObject
    {
      private List<Object> list;
      public volatile int bitmap$0;
    
      public List<Object> list()
      {
        if (
          (this.bitmap$0 & 0x1) == 0);
        synchronized (this)
        {
          if (
            (this.bitmap$0 & 0x1) == 0) {
            this.list = initialize(Bar..MODULE$.bar()); this.bitmap$0 |= 1; } return this.list;
        }
      }
      public List<Object> initialize(Bar f) { Predef..MODULE$.println(new StringBuilder().append("initialize called with Bar: '").append(f.name()).append("' ...").toString());
        return Nil..MODULE$;
      }
    }
    

    The lazy val is just a method that checks if the variable is already set and either returns it, or sets it and then returns it. So your mixin is not taken into account at all. If you want that, you have to take care of the initialization yourself.

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

Sidebar

Related Questions

I have been trying to get CodeDom to work so I can dynamically create
I have been trying to get this program to work but so far having
I have been trying to get nServiceBus to work with Ninject 2.0 as the
I have been trying to get this to work correctly and I think I
I have been trying to get this to work for 12 hrs now and
I have been trying to get a2billing to work for the past 4 days
I have been trying to get this query to work but I am stumbed
I have been trying to get Flask to work on my webfaction server for
I have been trying to get getch to work in another program with no
I have been trying to get the Fusion Charts to work on Android 2.2

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.