What's the correct way to create anonymous subclasses in Pharo?

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

What's the correct way to create anonymous subclasses in Pharo?

Joachim Geidel
Hi everybody,

I am attempting to create subclasses of existing classes at runtime. They
must not be in their superclass' list of subclasses, because they have to be
garbage collected when they are no longer needed.

This is what I tried to create such a class in Pharo 1.0:

    newMeta := (Metaclass new)
        superclass: Rectangle class
            methodDictionary: MethodDictionary new
            format: Rectangle class format;
        yourself.
    newClass := (newMeta new)
        superclass: Rectangle
            methodDictionary: MethodDictionary new
            format: Rectangle format;
        setName: #MyRectangle;
        yourself.
    newClass new class == newClass  "--> false"

The result of "newClass new" seems to be an instance of Rectangle, not of
newClass. The test at the end of the code snippet answers false. When I
compile a new method in newClass and install it in newClass'
methodDictionary, an instance will not understand a message with the
method's selector.

Almost the same code will do what I want in VisualWorks:

| newMeta newClass |
   newMeta := (Metaclass new)
        superclass: Rectangle class;
        methodDictionary: MethodDictionary new;
        setInstanceFormat: Rectangle class format;
        yourself.
    newClass := (newMeta new)
        superclass: Rectangle;
        methodDictionary: MethodDictionary new;
        setInstanceFormat: Rectangle format;
        setName: #MyRectangle;
        yourself.
    newClass new class == newClass  "--> true"

Does anybody have an idea what the problem is and what can be done to
circumvent it?

Thanks in advance!
Joachim Geidel



_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Igor Stasenko
Do you really need to create a meta?

If not , then you can do just:

newClass := Rectangle clone.
newClass
  superclass: Rectangle;
  organization: nil;
  methodDict: MethodDictionary new.

On 23 May 2010 15:49, Joachim Geidel <[hidden email]> wrote:

> Hi everybody,
>
> I am attempting to create subclasses of existing classes at runtime. They
> must not be in their superclass' list of subclasses, because they have to be
> garbage collected when they are no longer needed.
>
> This is what I tried to create such a class in Pharo 1.0:
>
>    newMeta := (Metaclass new)
>        superclass: Rectangle class
>            methodDictionary: MethodDictionary new
>            format: Rectangle class format;
>        yourself.
>    newClass := (newMeta new)
>        superclass: Rectangle
>            methodDictionary: MethodDictionary new
>            format: Rectangle format;
>        setName: #MyRectangle;
>        yourself.
>    newClass new class == newClass  "--> false"
>
> The result of "newClass new" seems to be an instance of Rectangle, not of
> newClass. The test at the end of the code snippet answers false. When I
> compile a new method in newClass and install it in newClass'
> methodDictionary, an instance will not understand a message with the
> method's selector.
>
> Almost the same code will do what I want in VisualWorks:
>
> | newMeta newClass |
>   newMeta := (Metaclass new)
>        superclass: Rectangle class;
>        methodDictionary: MethodDictionary new;
>        setInstanceFormat: Rectangle class format;
>        yourself.
>    newClass := (newMeta new)
>        superclass: Rectangle;
>        methodDictionary: MethodDictionary new;
>        setInstanceFormat: Rectangle format;
>        setName: #MyRectangle;
>        yourself.
>    newClass new class == newClass  "--> true"
>
> Does anybody have an idea what the problem is and what can be done to
> circumvent it?
>
> Thanks in advance!
> Joachim Geidel
>
>
>
> _______________________________________________
> Pharo-users mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
>



--
Best regards,
Igor Stasenko AKA sig.

_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Joachim Geidel
Re: [Pharo-users] What's the correct way to create anonymous subclasses in Pharo? Am 23.05.10 15:15 schrieb Igor Stasenko:
>
Do you really need to create a meta? > > If not , then you can do just: > > newClass := Rectangle clone. > newClass >   superclass: Rectangle; >   organization: nil; >   methodDict: MethodDictionary new.
Unfortunately, this doesn’t solve the problem:

| newClass method |
newClass := Rectangle clone.
newClass
  superclass: Rectangle;
  organization: nil;
  methodDict: MethodDictionary new;
  setName: #MyRectangle.
newClass new class name. "--> #Rectangle"
newClass new class == newClass. "--> false"
method := (newClass compile: 'testGhost ^42'
        classified: nil
        notifying: nil
        trailer: newClass defaultMethodTrailer
        ifFail: [nil]) method.
newClass addSelectorSilently: #testGhost withMethod: method.
newClass canUnderstand: #testGhost. "--> true".
newClass new respondsTo: #testGhost. "--> false"
newClass new testGhost. "--> Message not understood, should answer 42"

The problem is not the MetaClass (I do need it), but that the new class doesn’t create instances of itself, which I find very, very strange.

Best regards,
Joachim Geidel

_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Igor Stasenko
On 23 May 2010 16:38, Joachim Geidel <[hidden email]> wrote:

> Am 23.05.10 15:15 schrieb Igor Stasenko:
>> Do you really need to create a meta? > > If not , then you can do just: >
>> > newClass := Rectangle clone. > newClass >   superclass: Rectangle; >
>>   organization: nil; >   methodDict: MethodDictionary new.
> Unfortunately, this doesn’t solve the problem:
>
> | newClass method |
> newClass := Rectangle clone.
> newClass
>   superclass: Rectangle;
>   organization: nil;
>   methodDict: MethodDictionary new;
>   setName: #MyRectangle.
> newClass new class name. "--> #Rectangle"
> newClass new class == newClass. "--> false"

^^^
That's really strange.
Have you tried to debug it?


> method := (newClass compile: 'testGhost ^42'
>         classified: nil
>         notifying: nil
>         trailer: newClass defaultMethodTrailer
>         ifFail: [nil]) method.
> newClass addSelectorSilently: #testGhost withMethod: method.
> newClass canUnderstand: #testGhost. "--> true".
> newClass new respondsTo: #testGhost. "--> false"
> newClass new testGhost. "--> Message not understood, should answer 42"
>
> The problem is not the MetaClass (I do need it), but that the new class
> doesn’t create instances of itself, which I find very, very strange.
>
> Best regards,
> Joachim Geidel
>
> _______________________________________________
> Pharo-users mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
>
>



--
Best regards,
Igor Stasenko AKA sig.

_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Joachim Geidel
Re: [Pharo-users] What's the correct way to create anonymous subclasses in Pharo? Yes, I tried to debug it, but I can’t step into basicNew, of course, which already returns an instance of the wrong class.

Joachim Geidel


Am 23.05.10 15:46 schrieb Igor Stasenko:

On 23 May 2010 16:38, Joachim Geidel <joachim.geidel@...> wrote: > | newClass method | > newClass := Rectangle clone. > newClass >   superclass: Rectangle; >   organization: nil; >   methodDict: MethodDictionary new; >   setName: #MyRectangle. > newClass new class name. "--> #Rectangle" > newClass new class == newClass. "--> false" ^^^ That's really strange. Have you tried to debug it?

_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Stéphane Ducasse
In reply to this post by Joachim Geidel
clone is depreacted.
On May 23, 2010, at 3:38 PM, Joachim Geidel wrote:

> Am 23.05.10 15:15 schrieb Igor Stasenko:
> > Do you really need to create a meta? > > If not , then you can do just: > > newClass := Rectangle clone. > newClass >   superclass: Rectangle; >   organization: nil; >   methodDict: MethodDictionary new.
> Unfortunately, this doesn’t solve the problem:
>
>> | newClass method |
>> newClass := Rectangle clone.
>> newClass
>>   superclass: Rectangle;
>>   organization: nil;
>>   methodDict: MethodDictionary new;
>>   setName: #MyRectangle.
>> newClass new class name. "--> #Rectangle"
>> newClass new class == newClass. "--> false"
>> method := (newClass compile: 'testGhost ^42'
>>         classified: nil
>>         notifying: nil
>>         trailer: newClass defaultMethodTrailer
>>         ifFail: [nil]) method.
>> newClass addSelectorSilently: #testGhost withMethod: method.
>> newClass canUnderstand: #testGhost. "--> true".
>> newClass new respondsTo: #testGhost. "--> false"
>> newClass new testGhost. "--> Message not understood, should answer 42"
>
> The problem is not the MetaClass (I do need it), but that the new class doesn’t create instances of itself, which I find very, very strange.
>
> Best regards,
> Joachim Geidel
> _______________________________________________
> Pharo-users mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users


_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Mariano Martinez Peck
Hi Joachim: if you figured out how to do it, it would be cool to write it down in the http://book.pharo-project.org/

let me know if you want to do it and I create an user for you

cheers

mariano

On Mon, May 24, 2010 at 9:29 AM, Stéphane Ducasse <[hidden email]> wrote:
clone is depreacted.
On May 23, 2010, at 3:38 PM, Joachim Geidel wrote:

> Am 23.05.10 15:15 schrieb Igor Stasenko:
> > Do you really need to create a meta? > > If not , then you can do just: > > newClass := Rectangle clone. > newClass >   superclass: Rectangle; >   organization: nil; >   methodDict: MethodDictionary new.
> Unfortunately, this doesn’t solve the problem:
>
>> | newClass method |
>> newClass := Rectangle clone.
>> newClass
>>   superclass: Rectangle;
>>   organization: nil;
>>   methodDict: MethodDictionary new;
>>   setName: #MyRectangle.
>> newClass new class name. "--> #Rectangle"
>> newClass new class == newClass. "--> false"
>> method := (newClass compile: 'testGhost ^42'
>>         classified: nil
>>         notifying: nil
>>         trailer: newClass defaultMethodTrailer
>>         ifFail: [nil]) method.
>> newClass addSelectorSilently: #testGhost withMethod: method.
>> newClass canUnderstand: #testGhost. "--> true".
>> newClass new respondsTo: #testGhost. "--> false"
>> newClass new testGhost. "--> Message not understood, should answer 42"
>
> The problem is not the MetaClass (I do need it), but that the new class doesn’t create instances of itself, which I find very, very strange.
>
> Best regards,
> Joachim Geidel
> _______________________________________________
> Pharo-users mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users


_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users


_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Stéphane Ducasse
In reply to this post by Joachim Geidel
can you enter a bug entry in the bug report?

Stef

On May 23, 2010, at 2:49 PM, Joachim Geidel wrote:

> Hi everybody,
>
> I am attempting to create subclasses of existing classes at runtime. They
> must not be in their superclass' list of subclasses, because they have to be
> garbage collected when they are no longer needed.
>
> This is what I tried to create such a class in Pharo 1.0:
>
>    newMeta := (Metaclass new)
>        superclass: Rectangle class
>            methodDictionary: MethodDictionary new
>            format: Rectangle class format;
>        yourself.
>    newClass := (newMeta new)
>        superclass: Rectangle
>            methodDictionary: MethodDictionary new
>            format: Rectangle format;
>        setName: #MyRectangle;
>        yourself.
>    newClass new class == newClass  "--> false"
>
> The result of "newClass new" seems to be an instance of Rectangle, not of
> newClass. The test at the end of the code snippet answers false. When I
> compile a new method in newClass and install it in newClass'
> methodDictionary, an instance will not understand a message with the
> method's selector.
>
> Almost the same code will do what I want in VisualWorks:
>
> | newMeta newClass |
>   newMeta := (Metaclass new)
>        superclass: Rectangle class;
>        methodDictionary: MethodDictionary new;
>        setInstanceFormat: Rectangle class format;
>        yourself.
>    newClass := (newMeta new)
>        superclass: Rectangle;
>        methodDictionary: MethodDictionary new;
>        setInstanceFormat: Rectangle format;
>        setName: #MyRectangle;
>        yourself.
>    newClass new class == newClass  "--> true"
>
> Does anybody have an idea what the problem is and what can be done to
> circumvent it?
>
> Thanks in advance!
> Joachim Geidel
>
>
>
> _______________________________________________
> Pharo-users mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users


_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: What's the correct way to create anonymous subclasses in Pharo?

Joachim Geidel
In reply to this post by Mariano Martinez Peck
Mariano,

Am 21.06.10 11:49 schrieb Mariano Martinez Peck:
> Hi Joachim: if you figured out how to do it, it would be cool to write it down
> in the http://book.pharo-project.org/

The problem is a bug in Squeak and Pharo, apparently at the VM level.
Subclasses of classes in the specialObjectsArray create instances of their
superclass when sent #new or #basicNew. I have already submitted a bug
report for Pharo and should probably enter a similar one for Squeak:

  http://code.google.com/p/pharo/issues/detail?id=2523

I have tested this only on Mac OS X 10.6, so it is possible that only the
Mac OS X VM has this problem. It should be easy to check if it happens with
other VMs too.

Concerning contributions to Pharo and Squeak: I will restrict my work to
JNIPort and won't get involved in any work on Pharo or Squeak. The reason is
that I have a very limited amount of time to spend on open source projects,
so I have to draw a strict borderline.

Cheers,
Joachim Geidel



_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users