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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:14:18+00:00 2026-05-24T06:14:18+00:00

Take this code: class Register(var value:Int = 0) { def getZeroFlag() : Boolean =

  • 0

Take this code:

  class Register(var value:Int = 0) {
      def getZeroFlag() : Boolean = (value & 0x80) != 0
  }

  object Register {
      implicit def reg2int(r:Register):Int = r.value
      implicit def bool2int(b:Boolean):Int = if (b) 1 else 0
  }

I want to use it like so:

val x = register.getZeroFlag + 10

but I am greeted with:

type mismatch; found : Boolean required: Int

What goes? Do I need to define a implicit taking a function that returns a bool?

  • 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-24T06:14:18+00:00Added an answer on May 24, 2026 at 6:14 am

    Here’s an example demonstrating how to use your implicits:

    object Test {
      val register = new Register(42)
      val x = 1 + register // implicitly calling reg2int from companion object
      val y = register - 1 // same
      // val z = register + 1 // doesn't work because "+" means string concatenation
    
      // need to bring bool2int implicit into scope before it can be used
      import Register._
      val w = register.getZeroFlag() + 2 // this works now
      val z2 = register + 1 // works because in-scope implicits have higher priority 
    }
    

    Two potentially non-obvious things here:

    • When seeking implicit conversions to or from an object of type Register, the compiler will look in the companion object Register. This is why we didn’t need to bring reg2int explicitly into scope for defining x and y. However, the conversion bool2int does need to be in scope because it’s not defined on the Boolean or Int companion object.
    • The method + is already defined on all objects to mean string concatenation via the implicit any2stringadd in scala.Predef. The definition val z is illegal because the implicit for string concatenation takes priority over reg2int (implicits found in companion objects are relatively low priority). However, the definition val z2 works because we’ve brought reg2int into scope, giving it higher priority.

    For more details about how the compiler searches for implicits, see Daniel Sobral’s very nice explanation: Where does Scala look for implicits?

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

Sidebar

Related Questions

Take this code: public class MyClass { private final Object _lock = new Object();
Take this code: struct mystruct { int var; mystruct() : var(0) {} }; int
take this simple code: class A{ public: virtual void foo() = 0; void x(){
Take a look at this code: public class Test { public static void main(String...
take a look at this example code: public class Comment { private Comment() {
Please take a look at this code: template<class T> class A { class base
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take a look at this code : public class Program { [import]IMain Main {get;
I am trying to compile this code: class OthelloState { public: // constructor Othello(int
Take this sample code: Class Foo ReadOnly name As String Public Sub New(name As

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.