Add methods to an instance at runtime

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

Add methods to an instance at runtime

Fernando Rodríguez
Hi,

How can you programatically add methods to a class in runtime?

Thanks


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Ian Bartholomew-19
Fernando,

> How can you programatically add methods to a class in runtime?

Leaving aside the question of why you want to (it's not normally a good
idea and _won't_ work in a deployed app), in Dolphin you can just pass
the source to the class in question and ask it to compile.  As in

Integer compile: 'fubar: anInteger
^self + anInteger'

Evaluating

1 fubar: 99

should now answer 100

If the compilation fails the original expression answers nil, otherwise
it answers a CompiledMethod instance.

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Thomas Gagne-2
In reply to this post by Fernando Rodríguez
In VW you can call the object's class' #compile:notifying: method to add
methods at runtime.

Fernando wrote:
> Hi,
>
> How can you programatically add methods to a class in runtime?
>
> Thanks
>


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Christopher J. Demers
In reply to this post by Fernando Rodríguez
"Fernando" <[hidden email]> wrote in message
news:[hidden email]...
> How can you programatically add methods to a class in runtime?

SomeClass compile: methodString.

Where methodString would be the text of the method you desired to create on
SomeClass.  You may need to set some options when you deploy the package to
keep access to the compiler in the image (I don't remember).  You will also
need to distribute the compiler DLL with your program.  I believe it is
called "DolphinCR005.DLL".

Chris


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

jarober
In reply to this post by Ian Bartholomew-19
You speak way, way too soon when you call it  a bad idea.  I add
methods to applications at runtime on a regular basis:

The blog server located here:

http://www.cincomsmalltalk.com/blog/blogView

whenever I have patches/changes/updates, I toss a filein into a patch
directory and kick the server to load it.  I do this after testing on
my local server.  It's a heck of a lot more productive than taking the
server down and up

I also update BottomFeeder:

http://www.cincomsmalltalk.com/BottomFeeder

by shipping parcels that can be downloaded by the client and loaded -
either at startup or dynamically at runtime.  This works nicely as
well.  Invoking the compiler directly at runtime is one way to do this,
but it's a lot simpler to just load (or re-load) a parcel.  In VW, you
can load a parcel over an existing one in memory and have it replace
all the old code.

This is a powerful capability that you can - and should - make use of.
Saying you shouldn't is part of the static/Java/C mindset, IMHO


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Chris Uppal-3
In reply to this post by Fernando Rodríguez
Fernando wrote:

> How can you programatically add methods to a class in runtime?

Do you mean that you want to add new methods to a class at runtime, or do you
mean that you want to add new methods that are specific to just /one/ object at
runtime ?

I suspect you mean that you want instance-specific methods.  If so, then Andy
has described the process (for Dolphin Smalltalk) here:

http://groups.google.co.uk/groups?threadm=abqg81%24khmi1%241%40ID-51044.news.dfncis.de

Basically, what you are doing is creating a new lightweight subclass of the
object's original class; adding whatever new methods you want to that; and then
mutating the object to be an instance of the new class.

It sounds a bit messy, and I suppose it /is/ a bit messy ;-) but it's easy to
wrap up in a slick little wrapper, and then you can just forget about the mess.

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Ian Bartholomew-19
In reply to this post by jarober
[hidden email] wrote:

> This is a powerful capability that you can - and should - make use of.

Agreed, I make use of the compiler myself in a number of tools.

Are you saying it's a technique you would use in a "normal" application,
store code as source strings and recompile it if/when the application
needs it.  Or create/modify a method dynamically to respond to input
from the user.

Whilst it can be done it's not something that I would recommend to
someone relatively new to Smalltalk, which is why I responded to
Fernando in the way I did by saying it's "not normally a good idea"

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Reinout Heeck-3
Ian Bartholomew wrote:

> [hidden email] wrote:
>
>> This is a powerful capability that you can - and should - make use of.
>
>
> Agreed, I make use of the compiler myself in a number of tools.
>
> Are you saying it's a technique you would use in a "normal" application,
> store code as source strings and recompile it if/when the application
> needs it.  Or create/modify a method dynamically to respond to input
> from the user.
>
> Whilst it can be done it's not something that I would recommend to
> someone relatively new to Smalltalk, which is why I responded to
> Fernando in the way I did by saying it's "not normally a good idea"
>

But but but....

when I was an St newbie the very first project I did was to create my
own browser. I find it perfectly normal for newbies to joist with the
compiler, closures, exceptions and so on.

So I wholeheartedly recommend it to someone relatively new to St, just
don't put too much of this trickery in code others need to maintain ;-)


R
-


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Fernando Rodríguez
In reply to this post by jarober
Could you explain the difference between the parcel and the compiler
approach? Thanks.


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Mike Anderson
In reply to this post by Reinout Heeck-3
Reinout Heeck wrote:
> Ian Bartholomew wrote:
> > [hidden email] wrote:
> >
> >> This is a powerful capability that you can - and should - make use
of.
> >
> >
> > Agreed, I make use of the compiler myself in a number of tools.
> >
> > Are you saying it's a technique you would use in a "normal"
application,
> > store code as source strings and recompile it if/when the
application
> > needs it.  Or create/modify a method dynamically to respond to
input

> > from the user.
> >
> > Whilst it can be done it's not something that I would recommend to
> > someone relatively new to Smalltalk, which is why I responded to
> > Fernando in the way I did by saying it's "not normally a good idea"
> >
>
> But but but....
>
> when I was an St newbie the very first project I did was to create my

> own browser. I find it perfectly normal for newbies to joist with the

> compiler, closures, exceptions and so on.
>
> So I wholeheartedly recommend it to someone relatively new to St,
just
> don't put too much of this trickery in code others need to maintain
;-)

Seconded. I would say that this is one of the first Smalltalk "wow"
experiences. Not to be missed.

Mike


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Thomas Gagne-2
In reply to this post by Ian Bartholomew-19
Ian Bartholomew wrote:

> [hidden email] wrote:
>
>> This is a powerful capability that you can - and should - make use of.
>
>
> Agreed, I make use of the compiler myself in a number of tools.
>
> Are you saying it's a technique you would use in a "normal" application,
> store code as source strings and recompile it if/when the application
> needs it.  Or create/modify a method dynamically to respond to input
> from the user.

We store smalltalk code in sybase to describe how to decipher an payment
description.

Of course it's also used in my command-line Smalltalk.


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

jarober
In reply to this post by Ian Bartholomew-19
Read my post.  I don't directly invoke the compiler - I load new code
in, either via file in or parcel load.


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

jarober
In reply to this post by Fernando Rodríguez
The parcel approach is far easier to maintain.  Why?  Well, I create a
parcel by saving a package to disk.  A package is a Store (version
control) artifact.  Thus, I can query my runtime as to what version of
a parcel it has loaded, and compare that against the state of my source
code control system.  If I pass strings to the compiler, I'm in a
non-reproducible area.  Which is not to say that it should never be
done; it's just something you need to do with both eyes wide open


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Fernando Rodríguez
>Thus, I can query my runtime as to what version of
>a parcel it has loaded, and compare that against the state of my
source
>code control system.

Is  this a VW only thing, or is it standard? I'm using Dolphin.
I'd like to use this to update applicationsin a similar way as BF does.

Thanks


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

jarober
I'm sure you could use file-in support.  I don't know anything about
Dolphin's packaging scheme, so I can't talk to that


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Andy Bower-3
In reply to this post by Fernando Rodríguez
Fernando,

> > Thus, I can query my runtime as to what version of
> > a parcel it has loaded, and compare that against the state of my
> source
> > code control system.
>
> Is  this a VW only thing, or is it standard? I'm using Dolphin.
> I'd like to use this to update applicationsin a similar way as BF
> does.

Parcels are a VW "thing" but Dolphin does have something vaguely
similar in Binary Packages. Try doing a Google Groups search for
"Binary Packages Dolphin" for some ideas of how to use them and
arguments for and against.

It seems to me that you have three options to do what you want:

1) Distribute incremental updates to your application in source form in
much the same way that Dolphin's Live Update already does. Since your
application is probably going to be deployed as a read-only EXE rather
than the updateable image that the Dolphin development environment has
available then you will have to store down the source updates and load
then into the application each time it starts.

2) Use binary packages. Distribute your application as an EXE with a
core set of functionality that is unlikely to change plus a set of
binary packages that comprise the main functionality of the system.
Once again these packages will have to be loaded into the running EXE
each time the application starts. This shouldn't be a problem since the
binary packages are relatively quick to load. You can then arrange for
the binary packages to be updated/expanded by some download process of
your choosing.

3) Make your application a "Smalltalk Web Start" program. You can read
some details about how to do this here:

http://groups-beta.google.com/group/comp.lang.smalltalk.dolphin/browse_t
hread/thread/49c8314748bfd24c/ac0c3bce457fa516?q=smalltalk+webstart+dolp
hin&_done=%2Fgroups%3Fq%3Dsmalltalk+webstart+dolphin%26hl%3Den%26lr%3D%2
6c2coff%3D1%26rls%3DGGLD,GGLD:2004-08,GGLD:en%26sa%3DN%26tab%3Dwg%26&_do
neTitle=Back+to+Search&&d#ac0c3bce457fa516

Please be aware that the Dolphin Web Start framework was only intended
as a sample so if you choose to use it then you'll need to commit to
maintaining it yourself. The binary package stuff itself was originally
designed as part of the Dolphin web browser applet plugin.
Unfortunately, support for applets has now been discontinued (and
that's another story) but, as several people have asked, the binary
packages themselves will carry on being supported into the future.

I hope this helps,

Best regards,
 
Andy Bower
Dolphin Support
www.object-arts.com


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Schwab,Wilhelm K
Andy,

> 1) Distribute incremental updates to your application in source form in
> much the same way that Dolphin's Live Update already does. Since your
> application is probably going to be deployed as a read-only EXE rather
> than the updateable image that the Dolphin development environment has
> available then you will have to store down the source updates and load
> then into the application each time it starts.

Will that work in a deployed executable?  I tried it some time ago and
ran into all sorts of problems.  If it is indeed viable, I will add a
hook for it in a couple of apps that might benefit from it.  I would
probably not be comfortable with it for routine use (I would rather just
push out new installers of complete executables), but it could be very
helpful for patching problems in some situations.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Nevin Pratt
In reply to this post by Thomas Gagne-2
>
>
> We store smalltalk code in sybase to describe how to decipher an payment
> description.
>
> Of course it's also used in my command-line Smalltalk.

Yep.  There are lots of reasons to programmatically invoke the compiler.
  I've done it often.

A relatively recent example I've encountered is programmatically
building a GLORP query in a string (where the string in turn was created
based on user input on a GUI), and then compiling the string into a
block, because the GLORP API required a block.

For any method that requires a block for an argument, there are many
cases where you might want to build that block via a sequence of string
tokens, and then compile the final resultant string into a block.

Another example is the original expert system written for KnowledgeScape
(http://www.kscape.com).  It (again) would take user input from a
screen, build sequences of strings from that input, then compile the
strings into blocks, and feed the blocks into the expert system engine
(this was the original expert system-- not sure exactly what it does
now, but I'd guess the same basic approach is still being used).

Storing code in a database (such as Thomas is doing) is relatively
straight-forward with this approach, too.

There are lots of reasons to invoke the compiler.

Nevin


Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Bob Nemec
Our GemStone based financial system stores compiled blocks built from user
parameters.  So, if a set of three input numbers needs to be calculated as
(a / b) + c, the user enters that string and it gets compiled as a [:a :b :c
| (a / b) + c] block ... we then store the compiled block as an attribute of
the persistent function instance.

I'd like to see the Java boyz pull that stunt ;-)

Oh, and the code that does this is about six years old.
--
Bob Nemec
Northwater Objects

<...>
> A relatively recent example I've encountered is programmatically building
> a GLORP query in a string (where the string in turn was created based on
> user input on a GUI), and then compiling the string into a block, because
> the GLORP API required a block.
>
> For any method that requires a block for an argument, there are many cases
> where you might want to build that block via a sequence of string tokens,
> and then compile the final resultant string into a block.
<...>


Bob Nemec
Reply | Threaded
Open this post in threaded view
|

Re: Add methods to an instance at runtime

Andy Bower-3
In reply to this post by Schwab,Wilhelm K
Bill,

> > 1) Distribute incremental updates to your application in source
> > form in much the same way that Dolphin's Live Update already does.
> > Since your application is probably going to be deployed as a
> > read-only EXE rather than the updateable image that the Dolphin
> > development environment has available then you will have to store
> > down the source updates and load then into the application each
> > time it starts.
>
> Will that work in a deployed executable?  I tried it some time ago
> and ran into all sorts of problems.  If it is indeed viable, I will
> add a hook for it in a couple of apps that might benefit from it.  I
> would probably not be comfortable with it for routine use (I would
> rather just push out new installers of complete executables), but it
> could be very helpful for patching problems in some situations.

Well I think it should work but I'd be willing to be corrected. You'd
have to make sure that you turn off the deployment options that throw
away metadat from the image. Things like: "Strip meta-information",
"Strip class builder", "Retain instance variable names" and "Strip
empty method dictionaries".

--
Andy Bower
Dolphin Support
www.object-arts.com


12