CSI: Scala methods

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

CSI: Scala methods

Craig Tataryn
One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).

For instance, I'm using the mongo-scala-driver [1] and a statement like this:

        val transcripts = db.getCollection("transcripts") of Transcript

It can't tell me where "of" is defined.  Or:

        for {acct <- account from dbo} yield new Expense(acct)

It has no clue where "from" is declared.

Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?

Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.

Craig.

1 - http://github.com/osinka/mongo-scala-driver/wiki

--
Craig Tataryn
site: http://www.basementcoders.com/
podcast: http://www.basementcoders.com/?feed=podcast
itunes: http://itunes.apple.com/podcast/the-basement-coders
irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
twitter: @craiger

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: CSI: Scala methods

HamsterofDeath
 if the plugin can't figure it out, it's a bug. post it here:
http://youtrack.jetbrains.net/issues/SCL
and it will be fixed.

Am 20.10.2010 17:54, schrieb Craig Tataryn:

> One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).
>
> For instance, I'm using the mongo-scala-driver [1] and a statement like this:
>
> val transcripts = db.getCollection("transcripts") of Transcript
>
> It can't tell me where "of" is defined.  Or:
>
> for {acct <- account from dbo} yield new Expense(acct)
>
> It has no clue where "from" is declared.
>
> Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?
>
> Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.
>
> Craig.
>
> 1 - http://github.com/osinka/mongo-scala-driver/wiki
>
> --
> Craig Tataryn
> site: http://www.basementcoders.com/
> podcast: http://www.basementcoders.com/?feed=podcast
> itunes: http://itunes.apple.com/podcast/the-basement-coders
> irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
> twitter: @craiger
>
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: CSI: Scala methods

HamsterofDeath
In reply to this post by Craig Tataryn
 to solve your problem, press ctrl+shift+alt+n and enter the method name
you are looking for

Am 20.10.2010 17:54, schrieb Craig Tataryn:

> One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).
>
> For instance, I'm using the mongo-scala-driver [1] and a statement like this:
>
> val transcripts = db.getCollection("transcripts") of Transcript
>
> It can't tell me where "of" is defined.  Or:
>
> for {acct <- account from dbo} yield new Expense(acct)
>
> It has no clue where "from" is declared.
>
> Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?
>
> Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.
>
> Craig.
>
> 1 - http://github.com/osinka/mongo-scala-driver/wiki
>
> --
> Craig Tataryn
> site: http://www.basementcoders.com/
> podcast: http://www.basementcoders.com/?feed=podcast
> itunes: http://itunes.apple.com/podcast/the-basement-coders
> irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
> twitter: @craiger
>
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: CSI: Scala methods

Craig Tataryn
Nice, cmd+alt+shift+N on the Mac. Thanks! 

It found:

trait Implicits {
    implicit def collOfShape(coll: DBCollection) = new {
        def of[T](element: ObjectShape[T]) = element.collection(coll)
    }
//snip
}

Am I correct in my understanding that the implicit method colOfShape tells Scala:  "if someone tries to invoke a method on a DBCollection and the method doesn't exist, use me (collOfShape method) to convert the DBCollection to something else (my return type) then call the method on the returned value.  In this case the return value is an anonymous class with one method called "of"

Craig.

On 2010-10-20, at 1:36 PM, HamsterofDeath wrote:

to solve your problem, press ctrl+shift+alt+n and enter the method name
you are looking for

Am 20.10.2010 17:54, schrieb Craig Tataryn:
One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).

For instance, I'm using the mongo-scala-driver [1] and a statement like this:

val transcripts = db.getCollection("transcripts") of Transcript

It can't tell me where "of" is defined.  Or:

for {acct <- account from dbo} yield new Expense(acct)

It has no clue where "from" is declared.

Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?

Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.

Craig.

1 - http://github.com/osinka/mongo-scala-driver/wiki

--
Craig Tataryn
site: http://www.basementcoders.com/
podcast: http://www.basementcoders.com/?feed=podcast
itunes: http://itunes.apple.com/podcast/the-basement-coders
irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
twitter: @craiger




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: CSI: Scala methods

HamsterofDeath
my understanding of implicit methods is that  if you do this:
instanceOfClassA.methodOfClassB

the compiler will turn it into
implicitmethodWhichTakesInstanceOfClassAAndReturnsInstanceOfClassB(instanceOfClassA).methodOfClassB

the compiler will take care of calling the implicit method

in your case, some other magic is going on as the class returned is not explicitly defined, it's just something having the method of that you can call.

Am 20.10.2010 21:21, schrieb Craig Tataryn:
Nice, cmd+alt+shift+N on the Mac. Thanks! 

It found:

trait Implicits {
    implicit def collOfShape(coll: DBCollection) = new {
        def of[T](element: ObjectShape[T]) = element.collection(coll)
    }
//snip
}

Am I correct in my understanding that the implicit method colOfShape tells Scala:  "if someone tries to invoke a method on a DBCollection and the method doesn't exist, use me (collOfShape method) to convert the DBCollection to something else (my return type) then call the method on the returned value.  In this case the return value is an anonymous class with one method called "of"

Craig.

On 2010-10-20, at 1:36 PM, HamsterofDeath wrote:

to solve your problem, press ctrl+shift+alt+n and enter the method name
you are looking for

Am 20.10.2010 17:54, schrieb Craig Tataryn:
One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).

For instance, I'm using the mongo-scala-driver [1] and a statement like this:

val transcripts = db.getCollection("transcripts") of Transcript

It can't tell me where "of" is defined.  Or:

for {acct <- account from dbo} yield new Expense(acct)

It has no clue where "from" is declared.

Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?

Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.

Craig.

1 - http://github.com/osinka/mongo-scala-driver/wiki

--
Craig Tataryn
site: http://www.basementcoders.com/
podcast: http://www.basementcoders.com/?feed=podcast
itunes: http://itunes.apple.com/podcast/the-basement-coders
irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
twitter: @craiger





Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: CSI: Scala methods

Jason Zaugg
In reply to this post by Craig Tataryn
If you're not afraid of the command line, try compiling some Scala
code involving implicits with:

scalac -Xprint:typer file.scala

You will see the code with all the implicit view and implicit views
made explicit.

-jason

On Wed, Oct 20, 2010 at 5:54 PM, Craig Tataryn <[hidden email]> wrote:

> One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).
>
> For instance, I'm using the mongo-scala-driver [1] and a statement like this:
>
>        val transcripts = db.getCollection("transcripts") of Transcript
>
> It can't tell me where "of" is defined.  Or:
>
>        for {acct <- account from dbo} yield new Expense(acct)
>
> It has no clue where "from" is declared.
>
> Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?
>
> Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.
>
> Craig.
>
> 1 - http://github.com/osinka/mongo-scala-driver/wiki
>
> --
> Craig Tataryn
> site: http://www.basementcoders.com/
> podcast: http://www.basementcoders.com/?feed=podcast
> itunes: http://itunes.apple.com/podcast/the-basement-coders
> irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
> twitter: @craiger
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: CSI: Scala methods

Josh Suereth
In reply to this post by Craig Tataryn
This is correct to some extent.   The implicit keyword simply tells scala that the "implcit view" (that's what we call a method that converts one type to another) is available for use as an implicit.   The other part of this is that you have to have the implicit method be available in your local scope with no prefix, i.e.  The compiler need to be able to *just* write:    

colOfShape(db.getCollection("transcripts")) of Transcript

It will not write:

SomePrefix.colOfShape(db.getCollection("transcripts")) of Transcript.

So you either need to extend the Implicits so the conversion is available in your class, or you need to import it from some object.   Amusingly enough, you can remove implicits by defining your own empty function in the current scope with the same name as the implicit function.


Hope that helps!

On Wed, Oct 20, 2010 at 3:21 PM, Craig Tataryn <[hidden email]> wrote:
Nice, cmd+alt+shift+N on the Mac. Thanks! 

It found:

trait Implicits {
    implicit def collOfShape(coll: DBCollection) = new {
        def of[T](element: ObjectShape[T]) = element.collection(coll)
    }
//snip
}

Am I correct in my understanding that the implicit method colOfShape tells Scala:  "if someone tries to invoke a method on a DBCollection and the method doesn't exist, use me (collOfShape method) to convert the DBCollection to something else (my return type) then call the method on the returned value.  In this case the return value is an anonymous class with one method called "of"

Craig.

On 2010-10-20, at 1:36 PM, HamsterofDeath wrote:

to solve your problem, press ctrl+shift+alt+n and enter the method name
you are looking for

Am 20.10.2010 17:54, schrieb Craig Tataryn:
One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).

For instance, I'm using the mongo-scala-driver [1] and a statement like this:

val transcripts = db.getCollection("transcripts") of Transcript

It can't tell me where "of" is defined.  Or:

for {acct <- account from dbo} yield new Expense(acct)

It has no clue where "from" is declared.

Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?

Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.

Craig.

1 - http://github.com/osinka/mongo-scala-driver/wiki

--
Craig Tataryn
site: http://www.basementcoders.com/
podcast: http://www.basementcoders.com/?feed=podcast
itunes: http://itunes.apple.com/podcast/the-basement-coders
irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
twitter: @craiger





Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: CSI: Scala methods

Craig Tataryn
Thanks for the feedback.  Looks like Alex does it in the mongo-scala-driver by having the package object extend an Implicts trait (and mixes in the Trait which has the mysterious "of" method).

------
package com.osinka

import com.osinka.mongodb._

package object mongodb extends Implicits with shape.Implicits
------

Craig.

On 2010-10-21, at 7:10 AM, Josh Suereth wrote:

This is correct to some extent.   The implicit keyword simply tells scala that the "implcit view" (that's what we call a method that converts one type to another) is available for use as an implicit.   The other part of this is that you have to have the implicit method be available in your local scope with no prefix, i.e.  The compiler need to be able to *just* write:    

colOfShape(db.getCollection("transcripts")) of Transcript

It will not write:

SomePrefix.colOfShape(db.getCollection("transcripts")) of Transcript.

So you either need to extend the Implicits so the conversion is available in your class, or you need to import it from some object.   Amusingly enough, you can remove implicits by defining your own empty function in the current scope with the same name as the implicit function.


Hope that helps!

On Wed, Oct 20, 2010 at 3:21 PM, Craig Tataryn <[hidden email]> wrote:
Nice, cmd+alt+shift+N on the Mac. Thanks! 

It found:

trait Implicits {
    implicit def collOfShape(coll: DBCollection) = new {
        def of[T](element: ObjectShape[T]) = element.collection(coll)
    }
//snip
}

Am I correct in my understanding that the implicit method colOfShape tells Scala:  "if someone tries to invoke a method on a DBCollection and the method doesn't exist, use me (collOfShape method) to convert the DBCollection to something else (my return type) then call the method on the returned value.  In this case the return value is an anonymous class with one method called "of"

Craig.

On 2010-10-20, at 1:36 PM, HamsterofDeath wrote:

to solve your problem, press ctrl+shift+alt+n and enter the method name
you are looking for

Am 20.10.2010 17:54, schrieb Craig Tataryn:
One thing I'm finding hard with Scala development is figuring out where methods have been declared. I'm using IntelliJ X EAP in order to use the latest Scala plugin, and it really can't seem to gr0k where a lot of methods exist (i.e. no cmd-click hyper-link to the method declaration for me!).

For instance, I'm using the mongo-scala-driver [1] and a statement like this:

val transcripts = db.getCollection("transcripts") of Transcript

It can't tell me where "of" is defined.  Or:

for {acct <- account from dbo} yield new Expense(acct)

It has no clue where "from" is declared.

Also, because of Scala syntax I'm not entirely sure if those are imported methods from an Object or methods from the class the method proceeds?  For instance is "from" a method imported from an Object imported using SomeObject._ or a method of the account class?

Anyone have any tips and tricks for tracing down mystery methods, I'd greatly appreciate it.  So far, find . -name *.scala -exec grep ... has been my friend.

Craig.

1 - http://github.com/osinka/mongo-scala-driver/wiki

--
Craig Tataryn
site: http://www.basementcoders.com/
podcast: http://www.basementcoders.com/?feed=podcast
itunes: http://itunes.apple.com/podcast/the-basement-coders
irc: ThaDon on freenode #basementcoders, ##wicket, #papernapkin
twitter: @craiger






Loading...