adding methods to a class at runtime

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

adding methods to a class at runtime

Mark Volkmann
Is it possible to write code that adds methods to a class at runtime. In particular I'm thinking about something like attr_accessor in Ruby which adds get and set methods for instance variables so you don't have to write them.

Thanks to everybody who has answered my questions so far!

---
Mark Volkmann



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: adding methods to a class at runtime

David Mitchell-10
Sure, but Smalltalkers wouldn't wait for the first run of the program
to create the accessors. And, unlike Ruby, once you ran the create
accessor code, the accessors would be part of the code that goes under
version control.

This is one of the big differences between Ruby and Smalltalk. In
Smalltalk, there isn't much distinction between runtime and edit time.
Technically, there isn't any distinction. Even when you are editing
code, you are in some sense running the eventual application.

In fact, Smalltalkers often keep their application running as they make changes.

Take a look at:
CreateAccessorsForVariableRefactoring

Which you can invoke by right clicking on a class in the OmniBrowser
and choosing "accessors for instvar".

No reason you couldn't invoke such code at "runtime".

On 8/13/07, Mark Volkmann <[hidden email]> wrote:

> Is it possible to write code that adds methods to a class at runtime. In
> particular I'm thinking about something like attr_accessor in Ruby which
> adds get and set methods for instance variables so you don't have to write
> them.
>
> Thanks to everybody who has answered my questions so far!
>
> ---
> Mark Volkmann
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: adding methods to a class at runtime

Mark Volkmann
OmniBrowser looks interesting. I haven't installed anything new in my  
Squeak image yet. I'm guessing I just need to download the .st file  
for OmniBrowser and do a fileIn on it. Is there anything else I need  
to do to make it available in my image?

On Aug 13, 2007, at 10:37 AM, David Mitchell wrote:

> Sure, but Smalltalkers wouldn't wait for the first run of the program
> to create the accessors. And, unlike Ruby, once you ran the create
> accessor code, the accessors would be part of the code that goes under
> version control.
>
> This is one of the big differences between Ruby and Smalltalk. In
> Smalltalk, there isn't much distinction between runtime and edit time.
> Technically, there isn't any distinction. Even when you are editing
> code, you are in some sense running the eventual application.
>
> In fact, Smalltalkers often keep their application running as they  
> make changes.
>
> Take a look at:
> CreateAccessorsForVariableRefactoring
>
> Which you can invoke by right clicking on a class in the OmniBrowser
> and choosing "accessors for instvar".
>
> No reason you couldn't invoke such code at "runtime".
>
> On 8/13/07, Mark Volkmann <[hidden email]> wrote:
>> Is it possible to write code that adds methods to a class at  
>> runtime. In
>> particular I'm thinking about something like attr_accessor in Ruby  
>> which
>> adds get and set methods for instance variables so you don't have  
>> to write
>> them.

---
Mark Volkmann


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: adding methods to a class at runtime

David Mitchell-10
Forgot everyone isn't on OmniBrowser! The class I mentioned was
actually defined by the RefactoringBrowserEngine. Both Omni and the
RefactoringBrowser reference it.

FileIns are painful. I try to use Package Universes for all my
loading. If that wasn't an option, I would use the Installer. But
bootstrapping that into Squeak 3.9 is a fairly rough road.

Instead, I recommend starting with a base image that has it included.
This page is a great place to start:

http://damien.cassou.free.fr/squeak-dev.html

I'm using sq3.10-7137dev07.08.1.image, which is beta, but stable enough for me.

It includes OmniBrowser, eCompletion, Shout, and other niceties. It
also makes it easy to load other tools via Package Universes.

Getting back to your original question, The business end of creating
code at runtime is really just sending the message #compile: to a
class object. #compile: takes a String that is the method source to
compile.

The refactoring actually sends #compile:classified: which includes a
second string to set the category of the method (which makes sense for
a code browser, but might be overkill for runtime definition).

The send actually looks like this:

        definingClass
                compile: ('<1s><n><t>^ <2s>' expandMacrosWith: selector with: variableName)
                classified: #(#accessing).

That expand macro message asks String to substitute the variableName
in two places (1s and 2s with a newline, tab, return and a space in
between.

If you didn't want to use the refactoring browser, you could define
methods at runtime by sending #compile: to a class object. #compile:
just leaves the method uncategorized, which won't effect how the code
runs. Categories are essentially documentation, though they are also
by Monticello used for version management of class extensions (open
methods, in Ruby parlance).

You could even override doesNotUnderstand: (which Ruby calls
methodMissing) to see if it might be an accessor that hasn't been
defined yet. Then you wouldn't even need to declare the attr_accessor.
(Not that I'm recommending that style)

Whew! We're getting off the beginners topics now. On to squeak-dev!



On 8/13/07, Mark Volkmann <[hidden email]> wrote:

> OmniBrowser looks interesting. I haven't installed anything new in my
> Squeak image yet. I'm guessing I just need to download the .st file
> for OmniBrowser and do a fileIn on it. Is there anything else I need
> to do to make it available in my image?
>
> On Aug 13, 2007, at 10:37 AM, David Mitchell wrote:
>
> > Sure, but Smalltalkers wouldn't wait for the first run of the program
> > to create the accessors. And, unlike Ruby, once you ran the create
> > accessor code, the accessors would be part of the code that goes under
> > version control.
> >
> > This is one of the big differences between Ruby and Smalltalk. In
> > Smalltalk, there isn't much distinction between runtime and edit time.
> > Technically, there isn't any distinction. Even when you are editing
> > code, you are in some sense running the eventual application.
> >
> > In fact, Smalltalkers often keep their application running as they
> > make changes.
> >
> > Take a look at:
> > CreateAccessorsForVariableRefactoring
> >
> > Which you can invoke by right clicking on a class in the OmniBrowser
> > and choosing "accessors for instvar".
> >
> > No reason you couldn't invoke such code at "runtime".
> >
> > On 8/13/07, Mark Volkmann <[hidden email]> wrote:
> >> Is it possible to write code that adds methods to a class at
> >> runtime. In
> >> particular I'm thinking about something like attr_accessor in Ruby
> >> which
> >> adds get and set methods for instance variables so you don't have
> >> to write
> >> them.
>
> ---
> Mark Volkmann
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners