In the example for coding with Json using Databinder Dispatch Nathan uses an Object (Http) without a method, shown here:
import dispatch._
import Http._
Http("http://www.fox.com/dollhouse/" >>> System.out )
How is he doing this?
Thank you for all of the answers unfortunatly I was not specific enough...
It looks like it is simply passing an argument to a constructor of class or companion object Http.
In another example, I've seen another form:
http = new Http
http(/* argument here */)
Is this valid Scala? I guess it must be, because the author is a Scala expert. But it makes no sense to me. Actions are usually performed by invoking methods on objects, whether explicitly as object.doSomething() or implicitly as object = something (using the apply() method underneath the syntactic sugar).
All I can think of is that a constructor is being used to do something in addition to constructing an object. In other words, it is having side effects, such as in this case going off and doing something on the web.
I've seen another form:
val http = new Http http(/* argument here */)
Is this valid Scala? I guess it must be, because the author is a Scala expert. But it makes no sense to me.
In the first line, the Http class's constructor is invoked to create a new instance. Nothing different than in Java. In the second line, the Http class's apply method is invoked on that instance with (/* argument here */)
. That's obviously a Scala-specific thing.
There can be some confusion when the class has a companion object
with an apply
method. In that case (assuming that apply method just invokes the companion class's construtor), new instances can be created without the new
keyword:
val http = Http()
creates the new instance. This is an extremely common pattern, though companion object apply methods may do anything and have any signature.