0

When implementing Typeclasses for our types, we can use different syntaxes (an implicit val or an implicit object, for example). As an example:

A Typeclass definition:

  trait Increment[A] {
    def increment(value: A): A
  }

And, as far as I know, we could implement it for Int in the two following ways:

  implicit val fooInstance: Increment[Int] = new Increment[Int] {
    override def increment(value: Int): Int = value + 1
  }

// or

  implicit object fooInstance extends Increment[Int] {
    override def increment(value: Int): Int = value + 1
  }

I always use the first one as for Scala 2.13 it has an abbreviation syntax that looks like this:

implicit val fooInstance: Increment[Int] = (value: Int) => value + 1

But, is there any real difference between them? or is there any recommendation or standard to do this?

There is a related question about implicit defs and implicit classes for conversions, but I'm going more to the point of how to create (best practices) instances of Typeclasses, not about implicit conversions

Javier Montón
  • 4,601
  • 3
  • 21
  • 29
  • 1
    Does this answer your question? [Difference between conversion with implicit function and implicit class in Scala](https://stackoverflow.com/questions/32286628/difference-between-conversion-with-implicit-function-and-implicit-class-in-scala) – Johny T Koshy Nov 24 '21 at 10:37
  • 2
    I am not sure `implicit object` qualifies as case of `implicit class` as it doesn't imply implicit conversion. It stops at defining `object` and letting it be seen as `implicit` value. `implicit class` has this extra step where the class' constructor is turned into a implicit function (which could be used to perform a conversion). – Mateusz Kubuszok Nov 24 '21 at 11:12
  • 1
    Related: https://stackoverflow.com/questions/65258339/why-prefer-implicit-val-over-implicit-object/65258340#65258340 – Luis Miguel Mejía Suárez Nov 24 '21 at 14:23

1 Answers1

1

As far as I know the differences would be:

  • objects have different initialization rules - quite often they will be lazily initialized (it doesn't matter if you don't perform side effects in constructor)
  • it would also be seen differently from Java (but again, you probably won't notice that difference in Scala)
  • object X will have a type X.type which is a subtype of whatever X extends or implements (but implicit resolution would find that it extends your typeclass, though perhaps with a bit more effort)

So, I wouldn't seen any difference in the actual usage, BUT the implicit val version could generate less JVM garbage (I say garbage as you wouldn't use any of that extra compiler's effort in this particular case).

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64
  • Hey, I once created a wiki question / answer for exactly this: https://stackoverflow.com/questions/65258339/why-prefer-implicit-val-over-implicit-object/65258340#65258340 - I was thinking, should we close this one and port your answer to that wiki? _(or as a separate answer, whatever you prefer)_ - Let me know WDYT :) – Luis Miguel Mejía Suárez Nov 24 '21 at 14:24
  • I am fine with both :) – Mateusz Kubuszok Nov 24 '21 at 15:06
  • I didn't find your wiki question @LuisMiguelMejíaSuárez while searching for it, but your answer is really good. I'm fine with removing the question too and having both answers there, it seems cleaner in that way. – Javier Montón Nov 24 '21 at 15:13
  • 1
    @JavierMontón no worries, I know I am not popular :c - jokes aside, if you feel the question is a duplicate we can close it like that; the important thing is that we could help you ;) - Mateusz, could you please find some time to edit the wiki answer or add your own there? I will close this one as a duplicate as soon as I see you ported your answer there :D – Luis Miguel Mejía Suárez Nov 24 '21 at 15:29