# Can We Wean Scala Off Implicit Conversions?

**URL:** <https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388>\
**Category:** Language Design\
**Created:** [July 2, 2020, 9:51am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388 "2020-07-02T09:51:12Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![rssh](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rssh/32/745_2.png) [@rssh](https://contributors.scala-lang.org/u/rssh)\
**Post date:** [July 12, 2020, 12:29pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/82 "2020-07-12T12:29:23Z")

</div>

implicit conversions is still a best possible way, when you want to maintain two views of computation flow: one, coarse-grained when you don’t want to think about some details, which should be aligned automatically, and second - precise, where you want to control all details. Implicit conversions can be a layer which turn second into a first without boilerplate. Potential example:

```auto
async {
     import cps.implicitAwait // I don't want to think about details, like in Loom
     val urlData = fetchData( url )
     val theme = classify(urlData)
     retrieveDMPInfo(url, theme, userId)
}

```

and

```auto
async {
    // I want to color things themself
     val urlData = await(fetchData( url ))
     val theme = classify(urlData)
     await( retrieveDMPInfo(url, await(theme), userId) )
}

```

---

<div class="post-metadata">

**Author:** ![japgolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/japgolly/32/140_2.png) [@japgolly](https://contributors.scala-lang.org/u/japgolly)\
**Post date:** [July 15, 2020, 9:28am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/83 "2020-07-15T09:28:46Z")

</div>

I was very much in favour of this at first. I believe implicit conversions should be used as little as possible in non-test code. But they’re very, very, very useful in test code. They…

- help migrations (as mentioned above)
- enable conciseness - i want my non-test code as safe as possible which often means semantic layers for type-safety, but in tests it becomes boilerplate as you often manually construct data for verification and you don’t really care about type safety as the whole point of most tests is runtime value validation.
- DSLs - I’m personally not very interested in modelling code so that it reads like natural language but dsls are quite useful for dev UX and expressing complex concepts whilst reducing complexity from its construction. From time to time I find a good use cases in non-test code but in test code I find this way more useful, way more often. Implicit conversions often become a very important building block, often in a non-argument position meaning that typeclasses aren’t a drop-in replacement that preserves the DSL usage.

So while I’d be fine having implicit conversions removed for non-test code, I’d still want to specify a scalac flag or similar to retain implicit conversions for test code. It would be significantly detrimental when writing (and maintaining) tests, to be without implicit conversions.

---

<div class="post-metadata">

**Author:** ![rjolly](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rjolly/32/1737_2.png) [@rjolly](https://contributors.scala-lang.org/u/rjolly)\
**Post date:** [July 15, 2020, 1:50pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/84 "2020-07-15T13:50:14Z")

</div>

> [@lihaoyi](#):
>
> For people who want both together they can still get it by asking for both

I think `int2bigInt` is a case that requires both features : implicit constructor and extension method. How do you think we could handle it ?

---

<div class="post-metadata">

**Author:** ![sideeffffect](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sideeffffect/32/1466_2.png) [@sideeffffect](https://contributors.scala-lang.org/u/sideeffffect)\
**Post date:** [July 15, 2020, 10:57pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/85 "2020-07-15T22:57:34Z")

</div>

> [@odersky](#):
>
> So, without implicit conversions, we’d have better context information everywhere, we would avoid a lot of special cases and would avoid trying inference steps several times.

> [@odersky](#):
>
> In Scala 2, defining an implicit conversion requires a language import
> 
> ```auto
> import scala.language.implicitConversions
> 
> ```
> 
> Arguably, the language import should be at the use site instead. That’s where the surprises happen, and that’s where a language import could be a useful hint that something hidden goes on.

> [@japgolly](#):
>
> I was very much in favour of this at first. I believe implicit conversions should be used as little as possible in non-test code. But they’re very, very, very useful in…

> [@rssh](#):
>
> implicit conversions is still a best possible way, when you want…

So what if we kept `scala.language.implicitConversions` indefinitely but started requiring it in _use_ site? Then

- we would enjoy superior type inference or performance for the vast majority of the code, and
- for the rare cases where implicit conversions is what the author desires, be it tests or whatever else, she can have the import there, so it’s still possible.

Everybody wins 🎉

> [@smarter](#):
>
> people react to this sort of things by globally enabling implicitConversions in their build

I’m sure `sbt-tpolecat` maintainers would happily remove `-language:implicitConversions` if it meant better inference/performance for their users 😉 /cc @DavidGregory084

In other words, we won’t pay the price for implicit conversions, if we don’t use them 👍

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [July 16, 2020, 10:20am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/86 "2020-07-16T10:20:36Z")

</div>

I have the impression that a consensus is emerging to allow implicit conversions only under a language import at the use site. My original speculation that this option could at some point no longer be necessary has not born out. It looks there are sensible use cases to allow implicit conversions for prototyping and testing, but any such use cases should better be restricted to one project only.

Implicit conversions are useful as well for defining some interaction patterns between a library and its users, _if they are well designed_. But there’s an inherent problem with them: Normally, an API provides a contract between a library and its users. That contract should be as restricted as possible: it should just allow the required functionality and nothing extra. But implicit conversions in general violate this principle.A library defining them says in effect: “here are the methods you can call, and here is a bunch of extra things that make the calls more convenient, but that can also kick in in arbitrary places and interact strangely with other library uses”.

The primary example where these unwanted interactions are excluded by design is the magnet pattern. Here, the library defines its own magnet type and implicit conversions into that type. As long as every magnet type is restricted to a single library, and conversions into that type are only defined in the same library, interactions between different libraries are excluded. But the magnet types may not even share superclasses or traits except for Object; they have to be completely disjoint from one another. We could restrict implicit conversions to just that use case. Since the compiler has no idea what a project or library is we’d have to restrict conversions into a magnet type to be in the same source file, but that’s probably still OK. Define an implicit conversion to a class `M` to be _interference-free_ if (1) `M` does not have any base classes or traits except `Object`, `AnyVal`, or `Any` and (2) the conversion is in the same source file as the definition of `M`.

One could consider to still allow interference-free conversions without the language import. But I am not sure it’s worth it, since the use magnet pattern case can be addressed with type classes as well. Also, I am not sure whether type inference can improve if we still have to account for interference free conversions everywhere.

---

<div class="post-metadata">

**Author:** ![AndreVanDelft](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/andrevandelft/32/295_2.png) [@AndreVanDelft](https://contributors.scala-lang.org/u/AndreVanDelft)\
**Post date:** [July 16, 2020, 11:04am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/87 "2020-07-16T11:04:53Z")

</div>

It seems to be about a trade off between boiler plate code (if I may call `import language` clauses so) and rules guarding freedom from inference.

I would like to be able to use a set of implicit conversions and other imported things easily in multiple source files of a project. I would be happy if I could group multiple `import` clauses, including `import language` clauses, so that I would need only 1 line in a source file; then the need for explicitly importing implicit convertors would not bother me.

There has been discussion of such an `export` feature; I don’t know the current status. Do you think this would reduce the need for boiler plate code when using implicit conversions with the requirement of language import?

---

<div class="post-metadata">

**Author:** ![bmeesters](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bmeesters/32/2426_2.png) [@bmeesters](https://contributors.scala-lang.org/u/bmeesters)\
**Post date:** [July 16, 2020, 12:18pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/88 "2020-07-16T12:18:10Z")

</div>

> [@odersky](#):
>
> I have the impression that a consensus is emerging to allow implicit conversions only under a language import at the use site.

Would requiring such an import help the compiler with type inference speed & robustness when no import is present (and thus no implicit conversions are done in that scope)?

---

<div class="post-metadata">

**Author:** ![kai](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/kai/32/1733_2.png) [@kai](https://contributors.scala-lang.org/u/kai)\
**Post date:** [July 17, 2020, 10:48am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/89 "2020-07-17T10:48:03Z")

</div>

> [@odersky](#):
>
> The primary example where these unwanted interactions are excluded by design is the magnet pattern. Here, the library defines its own magnet type and implicit conversions into that type. As long as every magnet type is restricted to a single library, and conversions into that type are only defined in the same library, interactions between different libraries are excluded.

I don’t think magnet excludes library interactions – one of the advantages of magnet is that it IS extensible in other libraries, even without orphan implicits I can create my own types with conversions into the magnet. And this is something I do in reality, e.g. with `doobie` above, after converting it to magnet pattern I have defined a bunch of utility types in private code interacting with new magnet-powered interpolations by converting into the magnet type.

> [@AndreVanDelft](#):
>
> There has been discussion of such an `export` feature; I don’t know the current status. Do you think this would reduce the need for boiler plate code when using implicit conversions with the requirement of language import?

This feature is implemented in dotty, but I’m not sure if exporting `language.implicitConversions` will enable it, would be good if it did.

---

<div class="post-metadata">

**Author:** ![texasbruce](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/texasbruce/32/1077_2.png) [@texasbruce](https://contributors.scala-lang.org/u/texasbruce)\
**Post date:** [July 17, 2020, 4:26pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/90 "2020-07-17T16:26:59Z")

</div>

Is that something we can improve `export` by supporting a generic type parameter?

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [July 17, 2020, 5:07pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/91 "2020-07-17T17:07:55Z")

</div>

Hmmm… What about extension exports?

```scala
class A(val x: Int)
class B[T](val a: T, val y: Int)

extension (b : B[A]) 
  export b.a._ //an extension export
  def justSomeExtension : Unit = {} //an extension def

val a = new A(1)
val b = new B[A](a, 5)
println(b.x)

```

---

<div class="post-metadata">

**Author:** ![rgwilton](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rgwilton/32/1175_2.png) [@rgwilton](https://contributors.scala-lang.org/u/rgwilton)\
**Post date:** [July 18, 2020, 10:19am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/92 "2020-07-18T10:19:59Z")

</div>

I don’t know the full implications … but on the surface I quite like this suggestion.

The same behaviour could be achieved by a defining a explicit list of forwarder methods under the extension, so allowing export under an extension is really just a convenience that removes potential boilerplate.

---

<div class="post-metadata">

**Author:** ![rcano](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rcano/32/3073_2.png) [@rcano](https://contributors.scala-lang.org/u/rcano)\
**Post date:** [July 18, 2020, 5:39pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/93 "2020-07-18T17:39:41Z")

</div>

> - They make it hard to see what goes on in code. For instance, they might hide bad surprises like side effects or complex computations without any trace in the source code.
> - They usually give bad error diagnostics when they are not found, so code using them feels brittle and hard to change to developers not intimately familiar with the codebase.

How are these two points not true for the suggested alternatives, i.e typeclasses and magnets?

Type classes are more often than not provided by implicit defs, which incur the first point.  
The second point is so bad for type classes that I never recommend people learning scala any library that uses them:

- Type classes are not auto-completable. An IDE will never help you out with whatever combination of implicit defs you need in scope to craft that instance
- Scaladoc has no idea about them. No tool in scala-land helps you answer the question “how do I pass this value to this method provided that it requires this typeclass”. This is in fact so bad that a feature was introduced to scaladoc to **hide** the actual method signature in the collections api (CanBuildFrom), because the tool has no means to explain to you or shed any light into what that is.  
In java, when a method takes a type Foo and I don’t know how to get one, I go to javadoc for the type, hit usages, and I can find all the methods that return said type, which quickly reveals how to get one.
- It’s impossible to know if given the libraries that you have in scope (which for type class heavy libraries, this means following someone’s recommendation on what you need) you have your required typeclass already there, or it is derivable, or you have to implement one.
- Compiler errors when missing a type class are unhelpful, since typeclasses trigger prolog-style logic reasoning, and a valid derivation path may fail due to a missing part, but scalac has to abort the entire path and unhelpfully say “I just can’t find the typeclass for your value”
- You end up with the worst kind of documentation: booklets. These are non navigable (no immediate way to reach to what you want), non discoverable (you don’t know if the book will cover at any point what you need), non complete (you depend entirely on the author foreseeing your use case and having provided a snippet about it, and it won’t cover every single type because libraries grow faster than these booklets) books of prose that are a huge waste of time compared to javadoc.
- Reality is most people end up in a gitter simply asking one of the library authors how to proceed (or someone with equivalent knowledge). And Humans as a Service for documentation sounds horrible.

While the point about making things hard on the compiler still stands, and it’s worth discussing, I don’t find valid any of the arguments about implicit conversions making code hard and non obvious, unless we accept all of those are also true for type classes, which everyone has blessed.

---

<div class="post-metadata">

**Author:** ![Ichoran](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ichoran/32/40_2.png) [@Ichoran](https://contributors.scala-lang.org/u/Ichoran)\
**Post date:** [July 18, 2020, 7:22pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/94 "2020-07-18T19:22:41Z")

</div>

@rcano - I think your analysis is largely right. Typeclasses suffer from many of the same problems as implicit conversions.

However, the benefits you gain from typeclasses are enormous (in many instances). So one has to wonder whether implicit conversions also pull their weight sufficiently, even if you accept that typeclasses do.

My personal answer is yes. Other people seem to feel not. But I think you’re absolutely right that there will be painful aspects of typeclasses even if implicit conversions are completely removed…and also that the same fixes that would help typeclasses be more approachable may also help implicit conversions.

---

<div class="post-metadata">

**Author:** ![nafg](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nafg/32/38_2.png) [@nafg](https://contributors.scala-lang.org/u/nafg)\
**Post date:** [July 19, 2020, 4:28am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/95 "2020-07-19T04:28:00Z")

</div>

FWIW recently IntelliJ suggests imports that would supply missing implicits, and I does a much better job than before of autocompleting extension methods and then adding the import.

---

<div class="post-metadata">

**Author:** ![scalway](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scalway/32/670_2.png) [@scalway](https://contributors.scala-lang.org/u/scalway)\
**Post date:** [July 19, 2020, 10:53am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/96 "2020-07-19T10:53:24Z")

</div>

> [@odersky](#):
>
> since the use magnet pattern case can be addressed with type classes as well.

Great example of places where typeclasses cannot be used is `scalatags` library where you often write code like that:

```auto
div( //Takes Modifier*
  "Title", 
  Seq(1,2,3),
  someHtmlElement,
  h3(someRXVaule),
)

```

Each argument has different type and it cannot be written without implicit conversion (at least I don’t see it). It is also pretty common to write own conversions to Modifier (from Future/Rx/other) and currently it works seamlessly.

I understand that implicit conversions can be tricky when abused, but there are cases where they give us clean, readable code that works. I’ve hundreds of views written like that… ~15+ own types that implicitly converts to `Modifier` and there was no problem with them at all.

---

<div class="post-metadata">

**Author:** ![tarsa](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/tarsa/32/784_2.png) [@tarsa](https://contributors.scala-lang.org/u/tarsa)\
**Post date:** [July 19, 2020, 11:38am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/97 "2020-07-19T11:38:21Z")

</div>

> [@odersky](#):
>
> ### Implicit conversions are Evil
> 
> Implicit conversions are evil, for several reasons.
> 
> - They make it hard to see what goes on in code. For instance, they might hide bad surprises like side effects or complex computations without any trace in the source code.
> - They usually give bad error diagnostics when they are not found, so code using them feels brittle and hard to change to developers not intimately familiar with the codebase.
> - Type inference could be much better if there were no implicit conversions. Better means: faster, more powerful, and more predictable.

Maybe all above points could be removed by imposing a restriction on implicit conversion result? Instead of having:

```scala
@java.lang.FunctionalInterface
abstract class Conversion[-T, +U] extends Function1[T, U]

```

We could have:

```scala
@java.lang.FunctionalInterface // don't know if if will be still applicable
abstract class Conversion[-T, +U] extends (T => Conversion.Result[U]) {
  // restriction: can't make U directly, can only make Conversion.Result[U]
  final def apply(v: T): Conversion.Result[U] = Result.wrap(convert(v))
  protected abstract def convert(v: T): U
}

object Conversion {
  opaque type Result[+U] = U
  object Result {
    // make sure user code can't create Conversion.Result to ease inference
    private def wrap[U](r: U): Result[U] = r
  }
  def unwrap[U](r: Result[U]): U = r
}

```

That would make implicit conversion somewhat more explicit and painful to use, but it will keep most advantages of them anyway. However it wouldn’t work for ScalaFX use case described in [Can We Wean Scala Off Implicit Conversions? - #23 by kavedaa](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/23)

---

<div class="post-metadata">

**Author:** ![rssh](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rssh/32/745_2.png) [@rssh](https://contributors.scala-lang.org/u/rssh)\
**Post date:** [July 19, 2020, 1:37pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/98 "2020-07-19T13:37:32Z")

</div>

> imposing a restriction on implicit conversion result?

this will not help in example with implicit await (described above in this thread).

---

<div class="post-metadata">

**Author:** ![rcano](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rcano/32/3073_2.png) [@rcano](https://contributors.scala-lang.org/u/rcano)\
**Post date:** [July 19, 2020, 2:21pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/99 "2020-07-19T14:21:33Z")

</div>

> [@Ichoran](#):
>
> However, the benefits you gain from typeclasses are enormous (in many instances). So one has to wonder whether implicit conversions also pull their weight sufficiently, even if you accept that typeclasses do.

Yes indeed. I failed to convey my point, what I was trying to get at is that removing implicit conversions is like heaving a disease spreading through your body and chopping off a part of it where it has manifested the most visibly. You have not cured yourself, and the problem is rooted deeply in Scala (that’s what I was trying to show with the enumerated points).

Implicit conversions are a natural thing, everybody liked them before 2010 and the number of languages that has them is increasing, not reducing (furthermore, the languages that have added them are way more popular than Scala, more and more programmers are becoming familiar with this concept than with, say, type classes).

Nobody disliked their expressiveness, what people disliked was their ability (lack thereof) to get insights into the result (as has clearly been discussed in this thread that people fear them in code written by others, like libraries). These are the same problems that I showed for typeclasses. Dotty was a new compiler from scratch that essentially repeated the same mistake (my biased personal opinion) of scalac: being a traditional batch compiler whose job is turning directories of text into binary.

I’m o the opinion that a language as rich as scala, needs a compiler whose main task is answering questions about code, as fast as possible, and as rich as possible. I don’t think text is a good enough communication mean between Scala and the programmer. When a graph of implicits derivations fail to produce the type class I need, I want to see the fully typed graph of paths attempted by the compiler, with collapsible/expandable nodes, to understand why it failed. Most of the time I have a clear idea of the path I think the compiler should’ve taken, and I really wants answer as to why _that_ path failed. There’s no way to convey this information via text in a console without flooding it.

I don’t think scala failed at the features level, I think it failed (in a relative way, scala is by all means a success of a language) at the insights level. The richer the language, the more you should be able to communicate, but scalac and scaladoc don’t.

Summary: I believe killing off implicit conversions will deject people and make code invalid, providing no nice alternative, and not solving the problems for which you are killing them.

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [July 19, 2020, 5:45pm UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/100 "2020-07-19T17:45:15Z")

</div>

We went through this example [earlier](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/28).

---

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [July 20, 2020, 2:31am UTC](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388/101 "2020-07-20T02:31:50Z")

</div>

I do agree with @rcano that the problem is not so much implicit conversions, but orphan implicits in general. Implicits defined in companion objects, whether conversions or typeclasses, are generally foolproof and relatively discoverable. “orphaned” Implicits defined elsewhere are generally problematic, as they can be accidentally shadowed, forgotten to be imported, and are generally difficult to find.

Perhaps we should be discouraging orphan implicits in general?

[Previous page](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388.md?page=4)

[Next page](https://contributors.scala-lang.org/t/can-we-wean-scala-off-implicit-conversions/4388.md?page=6)
