Where is subsetOf in scala collections? - scala

I'm at a loss on this one...
Looking at a Set in Scala collections, I see that there is a method called subsetOf. But when I try to find where it is in the actual .scala source code (I've looked in Set.scala, GenSet.scala, SetLike.scala etc...) I can't find it!!!
Which trait actually defines that method? Or am I missing something?

If you click on function in scaladoc, you may see where is it defined:

The scala API specifies:
Definition Classes: GenSetLike
When you're looking at the API page, click the method entry to expand it. You'll see more information, including the "Definition Classes".
If you look at the source for GenSetLike.scala, you'll see it:
def subsetOf(that: GenSet[A]): Boolean = this forall that

Related

Variables documentation in Scala

I'm new in Scala (I come from Java) and I would like to know how is the correct way to generate documentation for the class variables.
For example, if I have the following code:
class MyClass (bar:bar) {
val foo = bar
def function {
...
...
}
}
what's the correct way to create the documentation for the variable foo ? Do I just add the comment right before the declaration? Isn't it a bit confusing?
Thanks!
You can use Javadoc in Scala. But Scala also introduces its own documentation generator called Scaladoc. This is what is used to generate the standard language documentation.
In general, Scaladoc follows similar conventions to Javadoc but introduces new features. You can read more about Scaladoc comments style here.
There is no difference between java and scala comments. You can choose any documentation strategy you used in java.

How to find documentation for Map

I'm trying to find documentation of the Map.toList method in Scala but looking at documentation this is a trait : http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map . So how can I find the documentation for scala Map? WHen I instatiate a Map am I just instantiating the trait ?
Trait scala.collection.immutable.Map is a contract. It's valid for all implementations, so its documentation is a documentation for any immutable scala Map.
In current implementation method Map.apply (Map(a -> b, c -> d, ...)) creates HashMap for more than 4 elements.
There are also classes Map1 - Map4 for 1-4 elements. Also there is a singleton EmptyMap.
But this behavior could be changed in next scala versions in case there will be better implementation for general purpose.
It is defined in Predef. Also its source might be useful.
Traits cannot be instantiated. They are abstract by definition. If they're actually fully implemented, they can (appear to) be instantiated by creating an anonymous type at the point of instantiation:
val x = new FullyImplementedTraitName { }
As to your main question, the documentation for scala.collection.Map should tell you everything you need to know. When you have the full frameset for the ScalaDocs displayed the filter text field at the upper left allows you to narrow down the class and package list by entering a the name (or portion thereof) you're looking for.

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.

How do I read this OCaml type signature?

I'm currently experimenting with using OCaml and GTK together (using the lablgtk bindings). However, the documentation isn't the best, and while I can work out how to use most of the features, I'm stuck with changing notebook pages (switching to a different tab).
I have found the function that I need to use, but I don't know how to use it. The documentation seems to suggest that it is in a sub-module of GtkPackProps.Notebook, but I don't know how to call this.
Also, this function has a type signature different to any I have seen before.
val switch_page : ([> `notebook ], Gpointer.boxed option -> int -> unit) GtkSignal.t
I think it returns a GtkSignal.t, but I have no idea how to pass the first parameter to the function (the whole part in brackets).
Has anyone got some sample code showing how to change the notebook page, or can perhaps give me some tips on how to do this?
What you have found is not a function but the signal. The functional type you see in its type is the type of the callback that will be called when the page switch happen, but won't cause it.
by the way the type of switch_page is read as: a signal (GtkSignal.t) raised by notebook [> `notebook ], whose callbacks have type Gpointer.boxed option -> int -> unit
Generally speaking, with lablgtk, you'd better stay away of the Gtk* low level modules, and use tge G[A-Z] higher level module. Those module API look like the C Gtk one, and I always use the main Gtk doc to help myself.
In your case you want to use the GPack.notebook object and its goto_page method.
You've found a polymorphic variant; they're described in the manual in Section 4.2, and the typing rules always break my head. I believe what the signature says is that the function switch_page expects as argument a GtkSignal.t, which is an abstraction parameterized by two types:
The first type parameter,
[> `notebook]
includes as values any polymorphic variant including notebook (that's what the greater-than means).
The second type parameter is an ordinary function.
If I'm reading the documentation for GtkSignal.t correctly, it's not a function at all; it's a record with three fields:
name is a string.
classe is a polymorphic variant which could be ``notebook` or something else.
marshaller is a marshaller for the function type Gpointer.boxed option -> int -> unit.
I hope this helps. If you have more trouble, section 4.2 of the manual, on polymorphic variants, might sort you out.

Does Scala have introspection capable of something similar to Python's dir()?

Yes, I know it's considered lazy by the non-Pythonistas. The reason I ask is that documentation is still woefully lacking in many Scala libraries (e.g. Scala-dbc, but that's not all I'm looking at), and if I could see the attributes of an object/class at runtime, I could at least figure out what's available. Thanks.
Scala does not have a reflection API. The only way to access this information is to use the Java reflection API. This has the disadvantage that the structure may change as the way Scala is represented in Java classes and interfaces may change in the future.
scala> classOf[AnyRef].getMethods
res0: Array[java.lang.reflect.Method] = Array(public final void ...
Some specific type information that is present in the byte code can be accessed with the ScalaSigParser.
import tools.scalap.scalax.rules.scalasig._
import scala.runtime._
val scalaSig = ScalaSigParser.parse(classOf[RichDouble])
That's one of my main uses for REPL. Type the object's name, dot, and then TAB and it will show all available methods.
It isn't perfect. For one thing, it shows protected methods, which won't be available unless you are extending the class. For another thing, it doesn't show methods available through implicit conversion.
And, of course, the IDEs are all capable of doing that.
You might want something like the following which would give you what you need. In this case, it operates on a String, obviously.
val testStr = "Panda"
testStr.getClass.getMethods.foreach(println)
Does that work?
You may want to use this little helper to beef up the REPL