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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:44:47+00:00 2026-05-15T23:44:47+00:00

I have this class in Scala: object Util { class Tapper[A](tapMe: A) { def

  • 0

I have this class in Scala:

object Util {
  class Tapper[A](tapMe: A) {
    def tap(f: A => Unit): A = {
      f(tapMe)
      tapMe
    }

    def tap(fs: (A => Unit)*): A = {
      fs.foreach(_(tapMe))
      tapMe
    }
  }

  implicit def tapper[A](toTap: A): Tapper[A] = new Tapper(toTap)
}

Now,

"aaa".tap(_.trim)

doesn’t compile, giving the error

error: missing parameter type for expanded function ((x$1) => x$1.trim)

Why isn’t the type inferred as String? From the error it seems that the implicit conversion does fire (otherwise the error would be along the lines of “tap is not a member of class String“). And it seems the conversion must be to Tapper[String], which means the type of the argument is String => Unit (or (String => Unit)*).

The interesting thing is that if I comment out either of tap definitions, then it does compile.

  • 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-15T23:44:48+00:00Added an answer on May 15, 2026 at 11:44 pm

    6.26.3 Overloading Resolution

    One first determines the set of
    functions that is potentially
    applicable based on the shape of the
    arguments

    …

    If there is precisely one alternative
    in B, that alternative is chosen.

    Otherwise, let S1, . . . , Sm be the
    vector of types obtained by typing
    each argument with an undefined
    expected type.

    Both overloads of tap are potentially applicable (based on the ‘shape’ of the arguments, which accounts for the arity and type constructors FunctionN).

    So the typer proceeds as it would with:

    val x = _.trim
    

    and fails.

    A smarter algorithm could take the least upper bound of the corresponding parameter type of each alternative, and use this as the expected type. But this complexity isn’t really worth it, IMO. Overloading has many corner cases, this is but another.

    But there is a trick you can use in this case, if you really need an overload that accepts a single parameter:

    object Util {
      class Tapper[A](tapMe: A) {
        def tap(f: A => Unit): A = {
          f(tapMe)
          tapMe
        }
    
        def tap(f0: A => Unit, f1: A => Unit, fs: (A => Unit)*): A = {
          (Seq(f0, f1) ++ fs).foreach(_(tapMe))
          tapMe
        }
      }
    
      implicit def tapper[A](toTap: A): Tapper[A] = new Tapper(toTap)
    
      "".tap(_.toString)
      "".tap(_.toString, _.toString)
      "".tap(_.toString, _.toString, _.toString)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have this case class define: class Protocol(protocol:String) object Protocol { def apply(protocol:String) :Protocol
Scala programmer should have known that this sort of writing : class Person{ var
I have a simple application: object Test extends App { implicit def t2mapper[X, X0
Simple question, I have code like this: class Context[A] { def t: A }
I have this class: Class B { private String D; private String E; }
I have this class file call SMSHelper.cs First I just wanted to know is
I have this class in which I run a for loop 10 times. This
I have this class which i would like to map: public class Contract {
I have this class: class OriginalClass { string FirstItem; List<string> ListOfSecondItems; } I want

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.