[squeak-dev] How to copy method safely?

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

[squeak-dev] How to copy method safely?

Sophie424
I have a class C1 with method M1 (source + compiled).

How do I safely make a copy of M1 in another class C2 (same source +
compile)?

Thanks - Sophie




Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: How to copy method safely?

Sophie424
I have tried several combinations of poking around #methodDictionary,
#addSelector, even CompiledMethod>>#getSource fed to Compiler. I only
succeed in messing up things and getting a large red methods-list pane in my
browser window :-)

Any suggestions appreciated!

"itsme213" <[hidden email]> wrote in message
news:gekg54$l9a$[hidden email]...

>I have a class C1 with method M1 (source + compiled).
>
> How do I safely make a copy of M1 in another class C2 (same source +
> compile)?
>
> Thanks - Sophie
>
>
>
>




Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: How to copy method safely?

Tapple Gao
On Sun, Nov 02, 2008 at 10:10:49PM -0600, Sophie (itsme213) wrote:

> I have tried several combinations of poking around #methodDictionary,
> #addSelector, even CompiledMethod>>#getSource fed to Compiler. I only
> succeed in messing up things and getting a large red methods-list pane in my
> browser window :-)
>
> Any suggestions appreciated!
>
> "itsme213" <[hidden email]> wrote in message
> news:gekg54$l9a$[hidden email]...
> >I have a class C1 with method M1 (source + compiled).
> >
> > How do I safely make a copy of M1 in another class C2 (same source +
> > compile)?
> >
> > Thanks - Sophie

Well, the easiest way is to copy-paste.

I suppose some people here might recommend the Refactoring
Browser, but I don't trust that thing. I don't know of a
pre-built way to do it, but I think this will work:

To copy class1 >> selector into class2:

class2
    compile: (class1 sourceCodeAt: selector)
    classified: (class1 organization categoryOfElement: selector)

What are you trying to do?

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: How to copy method safely?

Colin Putney
In reply to this post by Sophie424

On 2-Nov-08, at 8:10 PM, Sophie (itsme213) wrote:

> I have tried several combinations of poking around #methodDictionary,
> #addSelector, even CompiledMethod>>#getSource fed to Compiler. I only
> succeed in messing up things and getting a large red methods-list  
> pane in my
> browser window :-)

Something like this ought to work...

| source |
source := Class1 sourceCodeAt: #method1
Class2 compile: source classified: 'copied methods'



Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: Re: How to copy method safely?

Sophie424
In reply to this post by Tapple Gao
"Matthew Fulmer" <[hidden email]> wrote in message
> On Sun, Nov 02, 2008 at 10:10:49PM -0600, Sophie (itsme213) wrote:
>> I have tried several combinations of poking around #methodDictionary,
>> #addSelector, even CompiledMethod>>#getSource fed to Compiler. I only
>> succeed in messing up things and getting a large red methods-list pane in
>> my
>> browser window :-)

> Well, the easiest way is to copy-paste.

Ah, why didn't I think of that :-) I'd much prefer it programmed.

> What are you trying to do?

Migrate off traits to ease migration to Gemstone by (a) replacing the trait
with a plain class, and (b) programatically pushing methods from that class
into other classes.

> To copy class1 >> selector into class2:
>
> class2
>    compile: (class1 sourceCodeAt: selector)
>    classified: (class1 organization categoryOfElement: selector)
>

Many thanks, that did the trick !! Presumably it does the right thing with
clearing method caches etc.

Is there an equivalent way to reverse it i.e. remove a method with known
selector that I just added to class2?

- Sophie




Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: Re: How to copy method safely?

keith1y
Sophie (itsme213) wrote:

> "Matthew Fulmer" <[hidden email]> wrote in message
>  
>> On Sun, Nov 02, 2008 at 10:10:49PM -0600, Sophie (itsme213) wrote:
>>    
>>> I have tried several combinations of poking around #methodDictionary,
>>> #addSelector, even CompiledMethod>>#getSource fed to Compiler. I only
>>> succeed in messing up things and getting a large red methods-list pane in
>>> my
>>> browser window :-)
>>>      
>
>  
>> Well, the easiest way is to copy-paste.
>>    
>
> Ah, why didn't I think of that :-) I'd much prefer it programmed.
>
>  
>> What are you trying to do?
>>    
>
> Migrate off traits to ease migration to Gemstone by (a) replacing the trait
> with a plain class, and (b) programatically pushing methods from that class
> into other classes.
>  
Matthew has a script for flattening traits

Keith

Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: Traits on Gemstone

Tapple Gao
In reply to this post by Sophie424
On Sun, Nov 02, 2008 at 10:52:15PM -0600, Sophie (itsme213) wrote:

> "Matthew Fulmer" <[hidden email]> wrote in message
> > On Sun, Nov 02, 2008 at 10:10:49PM -0600, Sophie (itsme213) wrote:
> >> I have tried several combinations of poking around #methodDictionary,
> >> #addSelector, even CompiledMethod>>#getSource fed to Compiler. I only
> >> succeed in messing up things and getting a large red methods-list pane in
> >> my
> >> browser window :-)
>
> > Well, the easiest way is to copy-paste.
>
> Ah, why didn't I think of that :-) I'd much prefer it programmed.
>
> > What are you trying to do?
>
> Migrate off traits to ease migration to Gemstone by (a) replacing the trait
> with a plain class, and (b) programatically pushing methods from that class
> into other classes.

Traits support flattening, which means that you leave all the
traits methods in the class, but forget that they came from
elsewhere. To remove the traits from a class, but leave the
methods alone, I think you just do this:

aClass purgeLocalSelectors
aClass traitComposition: nil

If you need portability between Squeak and Gemstone, you should
probably just forget about using Traits. Flatten all your
classes using the above two lines, then delete all your traits.


However, if you still want to do your idea, you should be aware
that the protocol for building methods on different Smalltalks
is not standardized. Look inside Behavior, ClassDescription, and
Class inside Gemstone, and see what it does. Maybe you can
abstract it away.


To replace a trait with a plain class:
- make a class that has nothing but that trait
- flatten the class
- delete the trait

Here is the code:

Object subclass: #MyClass
    uses: MyTrait
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'MyStuff'

MyClass purgeLocalSelectors
MyClass traitComposition: nil

MyTrait removeFromSystem

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/

Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: Traits on Gemstone

Sophie424
"Matthew Fulmer" <[hidden email]> wrote in message

> If you need portability between Squeak and Gemstone, you should
> probably just forget about using Traits. Flatten all your
> classes using the above two lines, then delete all your traits.

In the (few) places that I have used traits, with quite simple usage, I've
found them a great boon. Were it not for traits not working on Gemstone, I
would have kept using them. Refactoring them entirely away would probably
either require lots of duplicate methods, or moving methods too high for my
liking (or too much hard re-thinking). Hence the home-brew approach!

Thanks for the caution about protocol differences, I will look out for that.
The 2-liner you gave me should port easily enough, and keep me going for a
while (famous last words :-)

- Sophie

p.s. as soon as I removed my traits from my code, my .mcz file now loads up
fine into current dev images; previously I would get an error as soon as my
trait was encountered, which I had posted about before but was not able to
narrow down further.




Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] How to copy method safely?

tblanchard
In reply to this post by Sophie424
You can just copy the source and paste it into the browser as source  
for the new class and accept it.

Or did you want to do this programmatically?

C2 copy: #m1 from: C1

(found in ClassDescription)

On Nov 2, 2008, at 7:14 AM, itsme213 wrote:

> I have a class C1 with method M1 (source + compiled).
>
> How do I safely make a copy of M1 in another class C2 (same source +
> compile)?
>
> Thanks - Sophie
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: Traits on Gemstone

Tapple Gao
In reply to this post by Tapple Gao
On Mon, Nov 03, 2008 at 03:58:35PM -0700, Matthew Fulmer wrote:

> On Sun, Nov 02, 2008 at 10:52:15PM -0600, Sophie (itsme213) wrote:
> > "Matthew Fulmer" <[hidden email]> wrote in message
> > > On Sun, Nov 02, 2008 at 10:10:49PM -0600, Sophie (itsme213) wrote:
> > >> I have tried several combinations of poking around #methodDictionary,
> > >> #addSelector, even CompiledMethod>>#getSource fed to Compiler. I only
> > >> succeed in messing up things and getting a large red methods-list pane in
> > >> my
> > >> browser window :-)
> >
> > > Well, the easiest way is to copy-paste.
> >
> > Ah, why didn't I think of that :-) I'd much prefer it programmed.
> >
> > > What are you trying to do?
> >
> > Migrate off traits to ease migration to Gemstone by (a) replacing the trait
> > with a plain class, and (b) programatically pushing methods from that class
> > into other classes.
>
> Traits support flattening, which means that you leave all the
> traits methods in the class, but forget that they came from
> elsewhere. To remove the traits from a class, but leave the
> methods alone, I think you just do this:
>
> aClass purgeLocalSelectors
> aClass traitComposition: nil

Here's a cleaner way to do it:

aClass flattenDownAllTraits

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/