Changing an Instance to another subclass 
Author Message
 Changing an Instance to another subclass

Hi Smalltalkers,

I have a problem and I don't know how to continue.

I have created a subclass of 'File' called 'MyFile' that adds some
useful features to 'File'.

Now I want to use 'Directory pathName: aPath files' to get all files
in a directory. But, of course, I get instances of 'File', not of my
great new subclass.

How can I get a collection of 'MyFile' from a directory? Do I have to
rewrite Directory>>pathName: or is there a way to change instances of
a certain class to instances of a subclass of that class?

Thanks for some hints.

Bye,
kai



Thu, 19 Mar 1998 03:00:00 GMT  
 Changing an Instance to another subclass

Kai> Hi Smalltalkers,

Kai> I have a problem and I don't know how to continue.

Kai> I have created a subclass of 'File' called 'MyFile' that adds some
Kai> useful features to 'File'.

Kai> Now I want to use 'Directory pathName: aPath files' to get all files
Kai> in a directory. But, of course, I get instances of 'File', not of my
Kai> great new subclass.

Kai> How can I get a collection of 'MyFile' from a directory? Do I have to
Kai> rewrite Directory>>pathName: or is there a way to change instances of
Kai> a certain class to instances of a subclass of that class?

The first question is, "why do you care?".  The things that
Directory>pathname: returns are only Files.  They have only the
behavior of Files.  They don't have the behavior of your MyFiles.

However, if you insist, I must ask how you create a MyFile from a File.
Say it is

        MyFile fromFile: aFile

then all you have to do is:

        (Directory pathName: aPath files) collect: [:f | MyFile fromFile: f]

and you'll get a collection of MyFiles, with original Files gone into
GC heaven.

Of course, my smalltalk is a little rusty, but I believe this is what
you want.

Just another budding smalltalker,
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Fri, 20 Mar 1998 03:00:00 GMT  
 Changing an Instance to another subclass
The previous response will sufice and is probably the most correct
solution. BUT a good hack might be:

aFile changeClassToThatOf: MyFile new.

provided MyFile instances add only behavior and no new structure...



Sun, 22 Mar 1998 03:00:00 GMT  
 Changing an Instance to another subclass


writes:

Quote:
> The first question is, "why do you care?".  The things that
> Directory>pathname: returns are only Files.  They have only the
> behavior of Files.  They don't have the behavior of your MyFiles.

> However, if you insist, I must ask how you create a MyFile from a
File.
> Say it is

>    MyFile fromFile: aFile

> then all you have to do is:

>    (Directory pathName: aPath files) collect: [:f | MyFile fromFile:
f]

> and you'll get a collection of MyFiles, with original Files gone
into
> GC heaven.

I think this is it. Thanks for this hint.

Bye,
kai



Tue, 24 Mar 1998 03:00:00 GMT  
 Changing an Instance to another subclass

Quote:

>The previous responses will sufice and is probably the most correct
>solution. BUT a good hack might be:

>aFile become:aMyFile

>which is simillar to swaping classes.

Indeed these are good hacks.  Speaking of a good solution instead,
using delegation here would be much better.

Changing classes as well as other such meta-operations in application-level
code are in 99% of cases symptoms of a wrong approach to the problem.  
#become: also has a bad reputation since object tables became (oops!)
a history.  They somehow remind the tricks from any Real Programmer's
assembly repertoire: jumps into the middle of instruction, tweaking
subprogram return address on the stack, etc.

--Vassili



Wed, 25 Mar 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to find a subclass' instance variables

2. VW3.0 - Instance variables in collection subclasses - Thanks for replies

3. VW3.0 -- Instance variables in collection subclasses

4. Converting an instance to a subclass?

5. Invoking a subclass's method on its superclass's instance

6. calling __init__ in a subclass of metaclass instance

7. create a new instance in C of a Python subclass

8. Duplication of instance variables in subclasses

9. Changes in superclass are not reflected in subclasses.

10. changing a list within a subclass of list ?

11. instances of classes and instances of instances

12. Changing Instance Names

 

 
Powered by phpBB® Forum Software