What does the symbol :+: mean in Scala Programming language?
object Devices {
private[endpoints] def endpoints(myService: MyService[Future], pService: PService[Future]) =
"devices" / (
banDevice(myService, pService) :+:
unbanDevice(myService, pService)
)
This is the code snippet. I'm using finagle library.
I've read that scala can have function names as symbols. Is this an example of that?
Where do I find the defention of this finagle defined methods?
Is it possible to override these methods?
Scala Does not have this function , it can be inside one of the libraries that you are using. According to your code , you might be using finagle !
Hence What Peter Neyens pointed out : https://github.com/finagle/finch/blob/master/docs/endpoint.md#coproduct-endpoints.
Seems to explain what you are trying to find out.
P.s: Its just a method in one of the libraries and nothing else.
In answer to your updated questions:
Yes, that code is calling the function (actually a method) named :+:.
You can probably find the API published online (maybe here).
If the class is not sealed and the method is not final then, yes, you should be able to override it.
Related
So the problem statement is as it says. I want to override the behaviour of equals in string class in a specific package.
I've looked around and mostly it seems there's no way to do this.
The closest i got to defining a method on an existing class was this -
implicit class StringImprovements(s: String) {
def increment = s.map(c => (c + 1).toChar)
}
and then use it like this -
"HAL".increment
Which is quite honestly amazing. But then i tried overriding equals using the same approach, it doesn't work.
I looked around and found this question from 2015 - override library method using Scala Implicit
Citing a line from above mentioned question -
Implicits are used if scala compiler cannot find method without it, so you can`t override methods with implicits
But given that scala releases are very frequent and things keep changing all the time, I was wondering whether it's possible to do so now, by this means or any other.
Thanks in advance!
No, it's not possible. And if it were, I would expect a lot of library code to break because it expected normal behavior from String#equals!
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.
On the official API page, I've searched for "time", but can't find the class. I looked at all methods on RichLong and RichInt, but can't find the methods.
I'm specifically talking about the methods that convert between int/long to some kind of rich object:
2 hours + 12 seconds
Note I'm not asking what the package is, I want to know how to find it.
Those aren't in the standard Scala API. Are you using the scala-time wrapper for JodaTime? If so, that would tell you where to look. In general, if you know which import enables the capability, it helps a lot when trying to find documentation!
If you know a method name, you can click on the little letters at the top of the left panel, just below the search text field in ScalaDoc--this will bring up a list of everything in the docs with that name, including methods (and tell you how to find it).
If a class doesn't have a method itself, you can use Scala to tell you what class it's getting converted to:
def whoHasSize[A](a: A)(implicit ev: A => { def size: Int }) = ev(a).getClass.getName
scala> whoHasSize("fish")
res1: java.lang.String = scala.collection.immutable.StringOps
So here you see that the string "fish" does not itself have a size method; instead, that's been granted by the scala.collection.immutable.StringOps method. You could do the same thing to find out about seconds.
Finally, stew's answer is probably what you were really looking for!
You can use the -Xlog-implicits flag to have the compiler show you where it is finding the implicit conversions.
I'm using Jerkson, and I need to check if a given class can be serialized. The java version just needs a class, but jerkson does this:
def canSerialize[A](implicit mf: Manifest[A]) = mapper.canSerialize(mf.erasure)
Given that I have an instance, how can I call this? I pretty much tried
canSerialize[ClassManifest.fromClass(foo)]
But its not working. I wonder why the guys at jerkson could not make it simpler by just making this: canSerialize(Class[_]) ...
Any ideas on how can I invoke this?
Edit:
I fixed this by using:
canSerilialize(Manifest.classType(foo.getClass))
How about this:
canSerialize[Foo]
Compiler can automatically generate manifest for you (if it has enough type information in context)
Since Scala 2.8.0 canSerialize can be written via context bound. See more
If you don't know the class in advance, you can always pass the manifest as a parameter, i.e. this should work: canSerialize( Manifest.classType( foo.getClass ) ).
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