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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:43:56+00:00 2026-05-17T16:43:56+00:00

I have seen a function named implicitly used in Scala examples. What is it,

  • 0

I have seen a function named implicitly used in Scala examples. What is it, and how is it used?

Example here:

scala> sealed trait Foo[T] { def apply(list : List[T]) : Unit }; object Foo {
     |                         implicit def stringImpl = new Foo[String] {
     |                             def apply(list : List[String]) = println("String")
     |                         }
     |                         implicit def intImpl = new Foo[Int] {
     |                             def apply(list : List[Int]) =  println("Int")
     |                         }
     |                     } ; def foo[A : Foo](x : List[A]) = implicitly[Foo[A]].apply(x)
defined trait Foo
defined module Foo
foo: [A](x: List[A])(implicit evidence$1: Foo[A])Unit

scala> foo(1)
<console>:8: error: type mismatch;
 found   : Int(1)
 required: List[?]
       foo(1)
           ^
scala> foo(List(1,2,3))
Int
scala> foo(List("a","b","c"))
String
scala> foo(List(1.0))
<console>:8: error: could not find implicit value for evidence parameter of type
 Foo[Double]
       foo(List(1.0))
          ^

Note that we have to write implicitly[Foo[A]].apply(x) since the compiler thinks that implicitly[Foo[A]](x) means that we call implicitly with parameters.

Also see How to investigate objects/types/etc. from Scala REPL? and Where does Scala look for implicits?

  • 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-17T16:43:56+00:00Added an answer on May 17, 2026 at 4:43 pm

    Here are a few reasons to use the delightfully simple method implicitly.

    To understand/troubleshoot Implicit Views

    An Implicit View can be triggered when the prefix of a selection (consider for example, the.prefix.selection(args) does not contain a member selection that is applicable to args (even after trying to convert args with Implicit Views). In this case, the compiler looks for implicit members, locally defined in the current or enclosing scopes, inherited, or imported, that are either Functions from the type of that the.prefix to a type with selection defined, or equivalent implicit methods.

    scala> 1.min(2) // Int doesn't have min defined, where did that come from?                                   
    res21: Int = 1
    
    scala> implicitly[Int => { def min(i: Int): Any }]
    res22: (Int) => AnyRef{def min(i: Int): Any} = <function1>
    
    scala> res22(1) // 
    res23: AnyRef{def min(i: Int): Int} = 1
    
    scala> .getClass
    res24: java.lang.Class[_] = class scala.runtime.RichInt
    

    Implicit Views can also be triggered when an expression does not conform to the Expected Type, as below:

    scala> 1: scala.runtime.RichInt
    res25: scala.runtime.RichInt = 1
    

    Here the compiler looks for this function:

    scala> implicitly[Int => scala.runtime.RichInt]
    res26: (Int) => scala.runtime.RichInt = <function1>
    

    Accessing an Implicit Parameter Introduced by a Context Bound

    Implicit parameters are arguably a more important feature of Scala than Implicit Views. They support the type class pattern. The standard library uses this in a few places — see scala.Ordering and how it is used in SeqLike#sorted. Implicit Parameters are also used to pass Array manifests, and CanBuildFrom instances.

    Scala 2.8 allows a shorthand syntax for implicit parameters, called Context Bounds. Briefly, a method with a type parameter A that requires an implicit parameter of type M[A]:

    def foo[A](implicit ma: M[A])
    

    can be rewritten as:

    def foo[A: M]
    

    But what’s the point of passing the implicit parameter but not naming it? How can this be useful when implementing the method foo?

    Often, the implicit parameter need not be referred to directly, it will be tunneled through as an implicit argument to another method that is called. If it is needed, you can still retain the terse method signature with the Context Bound, and call implicitly to materialize the value:

    def foo[A: M] = {
       val ma = implicitly[M[A]]
    }
    

    Passing a subset of implicit parameters explicitly

    Suppose you are calling a method that pretty prints a person, using a type class based approach:

    trait Show[T] { def show(t: T): String }
    object Show {
      implicit def IntShow: Show[Int] = new Show[Int] { def show(i: Int) = i.toString }
      implicit def StringShow: Show[String] = new Show[String] { def show(s: String) = s }
    
      def ShoutyStringShow: Show[String] = new Show[String] { def show(s: String) = s.toUpperCase }
    }
    
    case class Person(name: String, age: Int)
    object Person {
      implicit def PersonShow(implicit si: Show[Int], ss: Show[String]): Show[Person] = new Show[Person] {
        def show(p: Person) = "Person(name=" + ss.show(p.name) + ", age=" + si.show(p.age) + ")"
      }
    }
    
    val p = Person("bob", 25)
    implicitly[Show[Person]].show(p)
    

    What if we want to change the way that the name is output? We can explicitly call PersonShow, explicitly pass an alternative Show[String], but we want the compiler to pass the Show[Int].

    Person.PersonShow(si = implicitly, ss = Show.ShoutyStringShow).show(p)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen a function whose prototype is: int myfunc(void** ppt) This function is
I have seen examples of printing from a windows application but I have not
I have seen simple example Ajax source codes in many online tutorials. What I
I am new to php and i have seen rather Ambiguous function name convention
In my HTML file, I have a JavaScript function named CheckCaptcha. I am using
I have seen member functions programed both inside of the class they belong to
I have seen lots of questions recently about WPF... What is it? What does
I have seen Solutions created in Visual Studio 2008 cannot be opened in Visual
I have seen the references to VistaDB over the years and with tools like
I have seen this problem arise in many different circumstances and would like to

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.