Scala list with stackable modifications example - scala

I have read the following example on stackable modifications in scala: https://www.artima.com/pins1ed/traits.html#12.5
We have an abstract IntQueue whose put method is implemented in BasicQueue. BasicQueue gets modified by some traits that should modify the behavior of put. I don't understand why this works. If I understand linearization correctly, then BasicQueue is the first element in the linearization, therefore queue.put should call BasicQueue.put. I think that the implementation of BasicQueue.put should be in IntQueue.put which is the last method to call since IntQueue is the last element in the linearization. Then of course put shouldn't be implemented in BasicQueue.
Is my argumentation correct or where am I wrong.
ps: I don't have access to a computer with scala at the moment, otherwise I would try it out.

Related

Special grammar in scala

I am very new at Scala and Spark area, and I found a strange grammar usage in the scala inside the Apache beam project and I can't understand.
Here is the strange place:
JavaDStream<Metadata> metadataDStream = mapWithStateDStream.map(new Tuple2MetadataFunction());
// register ReadReportDStream to report information related to this read.
new ReadReportDStream(metadataDStream.dstream(), id, getSourceName(source, id), stepName)
.register();
From the above code, you can see inside the constructor of ReadReportDstream, the first parameter is
metadataDStream.dstream()
If we go inside the dstream() method, you will see the following code:
class JavaDStream[T](val dstream: DStream[T])(implicit val classTag: ClassTag[T])
extends AbstractJavaDStreamLike[T, JavaDStream[T], JavaRDD[T]] {
I am wondering why it uses "metadataDStream.dstream()" in the constructor instead of "metadataDStream.dstream"? What does the "()" do?
It's mostly a question of convention. Methods with empty parameter lists are evaluated for their side-effects. Methods without parameters are assumed to be purely functional, and free of side-effects. You can read more about that here - https://docs.scala-lang.org/style/method-invocation.html (Arity-0 section)
So in that case, we're probably having some side-effects in metadataDStream.dstream(). However, syntactically writing it as metadataDStream.dstream won't be an error.

this: SomeObject => object construct

I was reading up on best practices for implementing Slick, and was examining this example. In it, there is this construct:
trait BankRepository extends BankTable { this: DBComponent =>
... //A bunch of code
}
I don't understand the this: DBComponent => part. In this case, DBComponent is a simple trait defined elsewhere (you can find it in the above link). What I don't understand is:
What does the this: DBComponent => construct do. My IDE doesn't complain, but it also doesn't link to the function being executed by the =>. My intuition is that it's saying the rest of the code is a value that is returned, but I'm not clear on what is invoking it, or what the value returned exactly.
What do I even call this construct? As with many symbol-heavy constructs it's hard to look up/find documentation of, because it's clearly dependent on context. But even describing the context is difficult. What is this construct called?
It's called a self type. It's basically a contract that says any class extending this trait (mixing it in) must include DBComponent. And, as such, the compiler should assume DBCompenent elements are in scope for the following code.
Here's a link to a description of it from Programming in Scala, Odersky et al, 1st Edition (a little dated but still accurate on most topics).

What does the "extends {..}" clause in Scala object definition, without superclass name, do?

I found this code example in Programming in Scala, 2nd Ed. (Chapter 25, Listing 25.11):
object PrefixMap extends {
def empty[T] = ...
def apply[T](kvs: (String, T)*): PrefixMap[T] = ...
...
}
Why is the extends clause there without a superclass name? It looks like extending an anonymous class, but for what purpose? The accompanying text doesn't explain or even mention this construct anywhere. The code actually compiles and apparently works perfectly with or without it.
OTOH I found the exact same code on several web pages, including this (which looks like the original version of the chapter in the book). I doubt that a typo could have passed below the radars of so many readers up to now... so am I missing something?
I tried to google it, but struggled even to find proper search terms for it. So could someone explain whether this construct has a name and/or practical use in Scala?
Looks like a print error to me. It will work all the same, though, which probably helped hide it all this time.
Anyway, that object is extending a structural type, though it could also be an early initialization, if you had with XXX at the end. MMmmm. It looks more like an early initialization without any class or trait to be initialized later, actually... structure types do not contain code, I think.

Common in scala's Array and List

I'm new to scala(just start learning it), but have figured out smth strange for me: there are classes Array and List, they both have such methods/functions as foreach, forall, map etc. But any of these methods aren't inherited from some special class(trait). From java perspective if Array and List provide some contract, that contract have to be declared in interface and partially implemented in abstract classes. Why do in scala each type(Array and List) declares own set of methods? Why do not they have some common type?
But any of these methods aren't inherited from some special class(trait)
That simply not true.
If you open scaladoc and lookup say .map method of Array and List and then click on it you'll see where it is defined:
For list:
For array:
See also info about Traversable and Iterable both of which define most of the contracts in scala collections (but some collections may re-implement methods defined in Traversable/Iterable, e.g. for efficiency).
You may also want to look at relations between collections (scroll to the two diagrams) in general.
I'll extend om-nom-nom answer here.
Scala doesn't have an Array -- that's Java Array, and Java Array doesn't implement any interface. In fact, it isn't even a proper class, if I'm not mistaken, and it certainly is implemented through special mechanisms at the bytecode level.
On Scala, however, everything is a class -- an Int (Java's int) is a class, and so is Array. But in these cases, where the actual class comes from Java, Scala is limited by the type hierarchy provided by Java.
Now, going back to foreach, map, etc, they are not methods present in Java. However, Scala allows one to add implicit conversions from one class to another, and, through that mechanism, add methods. When you call arr.foreach(println), what is really done is Predef.refArrayOps(arr).foreach(println), which means foreach belongs to the ArrayOps class -- as you can see in the scaladoc documentation.

Searching inside scala 2.10 ASTs

What's the best way to recursively search for an element in scala 2.10 ASTs?
The trees might be a result of power.trees(code) or mirror.mkToolBox().parseExpr(code)
Edit. In 2.10.0-RC1 parseExpr has been renamed to parse.
The concrete use-case that I have is extracting the code of a method from a given class/object code by method name,
but I assume that the question would be more relevant for others if formulated in a more generic way.
Maybe you should have a look at https://github.com/scala/scala/blob/2.10.x/src/reflect/scala/reflect/api/Trees.scala#L606, especially at the classes Traverser, Transformer and the methods for substitution (Tree.substituteSymbols, Tree.substituteTypes or Tree.substituteThis). If you want to extract a method from a tree, you can use a Traverser and override the traverse method. In the traverse method, you check whether the node matches the method you want. If so, you are done. If not, you call super.traverse.