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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:05:35+00:00 2026-05-22T03:05:35+00:00

Are there any style guides that describe how to write code using Scala implicits?

  • 0

Are there any style guides that describe how to write code using Scala implicits?

Implicits are really powerful, and therefore can be easily abused. Are there some general guidelines that say when implicits are appropriate and when using them obscures code?

  • 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-22T03:05:36+00:00Added an answer on May 22, 2026 at 3:05 am

    I don’t think there is a community-wide style yet. I’ve seen lots of conventions. I’ll describe mine, and explain why I use it.

    Naming

    I call my implicit conversions one of

    implicit def whatwehave_to_whatwegenerate
    implicit def whatwehave_whatitcando
    implicit def whatwecandowith_whatwehave
    

    I don’t expect these to be used explicitly, so I tend towards rather long names. Unfortunately, there are numbers in class names often enough so the whatwehave2whatwegenerate convention gets confusing. For example: tuple22myclass–is that Tuple2 or Tuple22 you’re talking about?

    If the implicit conversion is defined away from both the argument and result of the conversion, I always use the x_to_y notation for maximum clarity. Otherwise, I view the name more as a comment. So, for instance, in

    class FoldingPair[A,B](t2: (A,B)) {
      def fold[Z](f: (A,B) => Z) = f(t2._1, t2._2)
    }
    implicit def pair_is_foldable[A,B](t2: (A,B)) = new FoldingPair(t2)
    

    I use both the class name and the implicit as a sort of a comment about what the point of the code is–namely to add a fold method to pairs (i.e. Tuple2).

    Usage

    Pimp-My-Library

    I use implicit conversions the most for pimp-my-library style constructions. I do this all over the place where it adds missing functionality or makes the resulting code look cleaner.

    val v = Vector(Vector("This","is","2D" ...
    val w = v.updated(2, v(2).updated(5, "Hi"))     // Messy!
    val w = change(v)(2,5)("Hi")                    // Okay, better for a few uses
    val w = v change (2,5) -> "Hi"                  // Arguably clearer, and...
    val w = v change ((2,5) -> "Hi", (2,6) -> "!")) // extends naturally to this!
    

    Now, there is a performance penalty to pay for implicit conversions, so I don’t write code in hotspots this way. But otherwise, I am very likely to use a pimp-my-library pattern instead of a def once I go above a handful of uses in the code in question.

    There is one other consideration, which is that tools are not as reliable yet at showing where your implicit conversions come from as where your methods come from. Thus, if I’m writing code that is difficult, and I expect that anyone who is using or maintaining it is going to have to study it hard to understand what is required and how it works, I–and this is almost backwards from a typical Java philosophy–am more likely to use PML in this fashion to render the steps more transparent to a trained user. The comments will warn that the code needs to be understood deeply; once you understand deeply, these changes help rather than hurt. If, on the other hand, the code’s doing something relatively straightforward, I’m more likely to leave defs in place since IDEs will help me or others quickly get up to speed if we need to make a change.

    Avoiding explicit conversions

    I try to avoid explicit conversions. You certainly can write

    implicit def string_to_int(s: String) = s.toInt
    

    but it’s awfully dangerous, even if you seem to be peppering all your strings with .toInt.

    The main exception I make is for wrapper classes. Suppose, for example, you want to have a method take classes with a pre-computed hash code. I would

    class Hashed[A](private[Hashed] val a: A) {
      override def equals(o: Any) = a == o
      override def toString = a.toString
      override val hashCode = a.##
    }
    object Hashed {
      implicit def anything_to_hashed[A](a: A) = new Hashed(a)
      implicit def hashed_to_anything[A](h: Hashed[A]) = h.a
    }
    

    and get back whatever class I started with either automatically or, at worst, by adding a type annotation (e.g. x: String). The reason is that this makes wrapper classes minimally intrusive. You don’t really want to know about the wrapper; you just need the functionality sometimes. You can’t completely avoid noticing the wrapper (e.g. you can only fix equals in one direction, and sometimes you need to get back to the original type). But this often lets you write code with minimal fuss, which is sometimes just the thing to do.

    Implicit parameters

    Implicit parameters are alarmingly promiscuous. I use default values whenever I possibly can instead. But sometimes you can’t, especially with generic code.

    If possible, I try to make the implicit parameter be something that no other method would ever use. For example, the Scala collections library has a CanBuildFrom class that is almost perfectly useless as anything other than an implicit parameter to collections methods. So there is very little danger of unintended crosstalk.

    If this is not possible–for example, if a parameter needs to be passed to several different methods, but doing so really distracts from what the code is doing (e.g. trying to do logging in the middle of arithmetic), then rather than make a common class (e.g. String) be the implicit val, I wrap it in a marker class (usually with an implicit conversion).

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

Sidebar

Related Questions

Are there any utilities that reformat Delphi code ? EDIT I am using Delphi
In general, I try to avoid using any style sheets and the like. But
Cocoa is well-documented and there is a lot of information on writing Cocoa code
Whitespace is signification in Python in that code blocks are defined by their indentation.
Is there any way to invert every color set within a parent div or
Is there any way to vertical align the photos in this case with CSS?
Is there any way to get the requested path (the path displayed in the
When we are applying a lot of style changes using JavaScript to a single
I have been using ojective c for almost a week, and I am mainly
This question is about variable naming style in objective c and cocoa. I just

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.