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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:48:31+00:00 2026-06-19T02:48:31+00:00

I make extensive use of the Pimp my Library pattern, and I’d like to

  • 0

I make extensive use of the Pimp my Library pattern, and I’d like to remove the boilerplate. For example, say I have some trait PrettyPrint:

trait PrettyPrint { def prettyPrint: String }

If I want to pimp Int and Double, I need to write code like this:

implicit def int2PrettyPrint(self: Int) = 
  new PrettyPrint { def prettyPrint = "Int: " + self }
implicit def double2PrettyPrint(self: Double) = 
  new PrettyPrint { def prettyPrint = "Double: " + self }

In the above, I’d classify as boilerplate: 1) The name of the implicit conversion, 2) The “new” keyword, 3) Perhaps the argument name “self”, 4) Perhaps the “implicit” keyword. I’d rather write something like this:

@pimp[Int, PrettyPrint] { def prettyPrint = "Int: " + self }
@pimp[Double, PrettyPrint] { def prettyPrint = "Double: " + self }

On the right hand sides of the above code, the name “self” is assumed to be the conversion argument.

Ideas on how to do this?

Some notes:

1) I’m amenable to using Scala 2.10 if necessary.

2) The new implicit classes in Scala 2.10 don’t suffice as far as I can tell. This is because there is only one implicit conversion for each implicit class. In other words, code like the following wouldn’t compile because PrettyPrint is declared twice:

implicit class PrettyPrint(self: Int) = ...
implicit class PrettyPrint(self: Double) = ...
  • 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-19T02:48:32+00:00Added an answer on June 19, 2026 at 2:48 am

    Follow-up on our discussion on NativeLibs4Java’s mailing list, where I gave an example of such a compiler plugin (which expands @extend(Int) def foo = blah into implicit class foo(self: Int) extends AnyVal { def foo = blah }).

    I’ve written a more elaborated plugin that expands these definitions into… macros (giving macro-expandable extensions / “pimps”, with no runtime dependency!).

    Given:

    @extend(Any) def quoted(quote: String): String = quote + self + quote
    

    It expands to:

    import scala.language.experimental.macros
    implicit class scalaxy$extensions$quoted$1(self: Any) {
      def quoted(quote: String) = macro scalaxy$extensions$quoted$1.quoted
    }
    object scalaxy$extensions$quoted$1 {
      def quoted(c: scala.reflect.macros.Context)
                (quote: c.Expr[String]): c.Expr[String] = {
        import c.universe._
        val Apply(_, List(selfTree$1)) = c.prefix.tree
        val self = c.Expr[Any](selfTree$1)
        {
          reify(quote.splice + self.splice + quote.splice)
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Web-Applications these days make extensive use of Javascript, for example various Google Products like
We have a MediaWiki wiki in which we make extensive use of templates. These
We use EventBrite for managing some free events: works great. We also make extensive
Some of my co-workers make extensive use of the VB.net concept of Modules. Unfortunately,
In my web-application, i make extensive use of a database. I have an abstract
I would like to start a new project which will make extensive use of
I make extensive use of Struts2 in my application. Now I want to add
I make pretty extensive use of PImpl and something that I've found myself waffling
I am looking for detailed ASP.NET MVC examples which make extensive use of grouped
In my experience I see a lot of architecture diagrams which make extensive use

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.