Proposal to Add Implied Instances to the Language

One weird thing I noticed when trying out Dotty in the REPL is the following:
I defined a trait:

scala> trait Semigroup[A] {
     |   def (a: A) combine (b: A): A
     | }

and then try to define an instance on String:

scala> given Semigroup[String] {
     |   def (a: String) combine (b: String): String = a + b
     | }

But get the super confusing error:

2 |  def (a: String) combine (b: String): String = a + b
  |                                                    ^
  |  Found:    String(b)
  |  Required: String'
  |
  |  where:    String  is a type in class Semigroup
  |            String' is a type in object Predef which is an alias of String

Well turns out I was missing the as keyword and I was suppose to actually type:

given as Semigroup[String] { .. }

Which is weird. I don’t know why the first one compiles. I assume it was translate into something akin to

class Semigroup[String] {
  def (a: String) combine (b: String): String = a + b
}

As in, the String there is a name of a type parameter, rather than java.String

3 Likes