using new keyword with def instead of classname

28 views
Skip to first unread message

Taimoor Hasnie

unread,
Nov 22, 2012, 6:35:02 AM11/22/12
to scala...@googlegroups.com
Hi,
I cant understand this code. Can somebody help me with understanding this please. Here is the link to the entire snippet http://code.google.com/p/orient/wiki/ScalaLanguage

object Test {
       
implicit def dbWrapper(db: ODatabaseObjectTx) = new {
               
def queryBySql[T](sql: String, params: AnyRef*): List[T] = {
                        val params4java
= params.toArray
                        val results
: java.util.List[T] = db.query(new OSQLSynchQuery[T](sql), params4java: _*)
                        results
.asScala.
toList
               
}
       
}
Q1. I havent seen the use of 'new' keyword like this.  What is new keyword achieving here? I have usually seen the usage like new SomeClass . Where new creates a new object for the mentioned classname. But here we have function...and this is confusing me.


Thanks,
Taimoor.






Daniel Sobral

unread,
Nov 22, 2012, 6:41:12 AM11/22/12
to Taimoor Hasnie, scala-user
Here is is creating an anonymous class -- just like in Java.
--
Daniel C. Sobral

I travel to the future all the time.

Roland Kuhn

unread,
Nov 22, 2012, 7:00:31 AM11/22/12
to Daniel Sobral, Taimoor Hasnie, scala-user
With the disadvantage that calls to queryBySql() will be dispatched using reflection, because the inferred return type of that method will be a structural one. (just sayin’)

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn


Damian Helme

unread,
Nov 22, 2012, 7:07:55 AM11/22/12
to scala...@googlegroups.com, Taimoor Hasnie
Also, he's using the 'pimp my library' pattern to effectively add the queryBySql method to ODatabaseObjectTX class. See here for more info:

Reply all
Reply to author
Forward
0 new messages