Using symbol

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Using symbol

frankl1_miky
Hi,

I'll like to know what is happening exactly in background when I'm using a symbol as in Collection withAllSubclasses collect: #numberOfLinesOfCode.

I also want to know the other cases where I can use symbol.

Is there any tutorial about using symbol?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Using symbol

Nicolas Passerini
Collection withAllSubclasses collect: #numberOfLinesOfCode

is the equicalent to

Collection withAllSubclasses collect: [:each | each numberOfLinesOfCode ]

What happens below is that (unary) symbols are polymorphic with one parameters blocks, i.e. they understand cull: and value:, which is implemented as:

value: anObject 
^anObject perform: self.

So in fact what will hapen is more like
Collection withAllSubclasses collect: [:each | each perform: #numberOfLinesOfCode ]

... which is a bit slower than the traditional block version so you might want to think if that makes a difference in your case.


You can use symbols whenever a unary block is expected, for example in most collection enumerating methods, such as do:, select:, reject:, allSatisfy:, detect:, ... (see 'enumerating' protocol in Collection class for more examples).

All this methods send value: (or maybe cull:) to the received block argument, so you can pass an unary symbol instead (or even whatever object that understands value:).



On Mon, Sep 19, 2016 at 12:12 PM, frankl1_miky <[hidden email]> wrote:
Hi,

I'll like to know what is happening exactly in background when I'm using a
symbol as in *Collection withAllSubclasses collect: #numberOfLinesOfCode*.

I also want to know the other cases where I can use symbol.

Is there any tutorial about using symbol?

Thanks



--
View this message in context: http://forum.world.st/Using-symbol-tp4916159.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: Using symbol

Ben Coman
In reply to this post by frankl1_miky
On Mon, Sep 19, 2016 at 6:12 PM, frankl1_miky
<[hidden email]> wrote:
> Hi,
>
> I'll like to know what is happening exactly in background when I'm using a
> symbol as in *Collection withAllSubclasses collect: #numberOfLinesOfCode*.

Nicolas has a good answer.
Now you could explore this yourself.  First, as a reference inspect
"Collection withAllSubclasses" on its own.
Then try debugging into the expression (from the context menu in the
Playground.)

In particular in OrderedCollection>>collect:  step-INTO   "aBlock
value: ....."


For an even lower level (probably too low), try debugging into...
OpalCompiler compile: 'Collection withAllSubclasses collect:
#numberOfLinesOfCode'

or more simply debug...
OpalCompiler compile: '#numberOfLinesOfCode inspect'.

Other things you could explore
* Look at the class comment for class Symbol
* Look at the tests of class SymbolTest
* In the Browser with class Symbol selected, right-click and choose
Analyze>Class refs.

cheers -ben

>
> I also want to know the other cases where I can use symbol.
>
> Is there any tutorial about using symbol?
>
> Thanks
>
>
>
> --
> View this message in context: http://forum.world.st/Using-symbol-tp4916159.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Using symbol

philippeback
In reply to this post by frankl1_miky
You can search for usages of Symbol. Lots of things to learn indeed.




On Mon, Sep 19, 2016 at 12:12 PM, frankl1_miky <[hidden email]> wrote:
Hi,

I'll like to know what is happening exactly in background when I'm using a
symbol as in *Collection withAllSubclasses collect: #numberOfLinesOfCode*.

I also want to know the other cases where I can use symbol.

Is there any tutorial about using symbol?

Thanks



--
View this message in context: http://forum.world.st/Using-symbol-tp4916159.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.