Adam Mackler
Adam Mackler

Reputation: 2080

How can I find the definition of methods/operations defined in an implicitly-converted class?

I'm looking at someone else's source code (Scala), where I see the operator :+= being called on a variable of type IndexedSeq. I am looking all over the scaladocs page for that class to figure out what that operator does, but I do not see it. I'm thinking that either it's defined in a class outside of IndexedSeq's inheritance hierarchy, or else the javascript on the scaladocs page is hiding it somewhere I can't see it. (Actually it's neither; see answer below.)

I've hit every button on the scaladocs page trying to unhide everything. I've looked in the web-page's HTML code. There has got to be a way to look up an operator from the documentation of a class to which it can be applied. Hasn't there?

(N.B.: I looked up that operator using symbolhound, so I know what that operator means now. This question is about scala documentation in general, not that particular operator.)

Upvotes: 7

Views: 128

Answers (2)

pedrofurla
pedrofurla

Reputation: 12783

Is this value assigned to a variable? If it's the case I think this syntax sugar:

scala> var x = IndexedSeq(1,2,3)
x: IndexedSeq[Int] = Vector(1, 2, 3)

scala> x :+= 10

scala> x
res59: IndexedSeq[Int] = Vector(1, 2, 3, 10)

scala> val y = IndexedSeq(1,2,3)
y: IndexedSeq[Int] = Vector(1, 2, 3)

scala> y :+= 10
<console>:16: error: value :+= is not a member of IndexedSeq[Int]
          y :+= 10
            ^

It is syntax sugar for "operation and assignment", like +=:

scala> var x = 10
x: Int = 10

scala> x += 1

scala> x
res63: Int = 11

Which de-sugars to x = x + 1.

Upvotes: 1

gourlaysama
gourlaysama

Reputation: 11290

All operators in Scala are normal methods.

You cannot find it because it is compiler magic for re-assignement, it is not an operator. Or to say it another way: it looks like an operator of its own, but it is actually "an operator followed by the = character".

The compiler will magically turn that into a assignment if the operator (here :+) returns the proper type, and the original value was a var, obviously.

Since it is not provided by any implicit nor explicit method on Seq[T] or whatever, it does not appear anywhere in the generated scaladoc.

So to answer the general question:

  • It is a language construct, so the only place where it is documented is the specification, sadly,
  • but, if you find some "<?>=" unknown operator somewhere, look for the definition of "<?>", that one is sure to be documented.

Edit: I finally found where this is defined in the SLS:

§6.12.4:

An assignment operator is an operator symbol (syntax category op in (§1.1)) that ends in an equals character “=”, with the exception of operators for which one of the following conditions holds:

(1) the operator also starts with an equals character, or

(2) the operator is one of (<=), (>=), (!=).

It also says later on that it only happens when all other options have been tried (including potential implicits).

Upvotes: 9

Related Questions