Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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.)

like image 517
Adam Mackler Avatar asked Jul 31 '13 19:07

Adam Mackler


People also ask

How do you define implicit conversion in C++?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What is the example of implicit conversion?

Implicit conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

What do you mean by implicit data conversion explain with example?

When an implicit conversion is done, it is not just a reinterpretation of the expression's value but a conversion of that value to an equivalent value in the new type. Consider the following example: float f = 3; // implicit conversion to float value 3.0 int i = 5.23f; // implicit conversion to integer value 5.

Which conversion can be performed implicitly?

Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.


2 Answers

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).

like image 143
gourlaysama Avatar answered Sep 25 '22 09:09

gourlaysama


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.

like image 30
pedrofurla Avatar answered Sep 24 '22 09:09

pedrofurla