[Pharo-dev] LearningObject

stepharo stepharo at free.fr
Wed Jan 27 14:42:01 EST 2016

Hi guys

I'm on holidays (no book no mooc no bugs just playing magic good music 
and hacking a bit)
and I wanted to implemented a fun object that learn from Object by 
copying the methods.

ProtoObject subclass: #ATMinimalLearningObject
     instanceVariableNames: ''
     classVariableNames: ''
     package: 'MiniActalk2'

ATMinimalLearningObject >> doesNotUnderstand: aMessage

     | sel |
     sel := aMessage selector.
     Transcript show: 'Does not understand ',  sel printString ; cr.
     ^ (Object includesSelector: sel)
         ifTrue: [
                 self addFromObjectSelector: sel.
                 aMessage sendTo: self ]
         ifFalse: [ super doesNotUnderstand: aMessage ].

ATMinimalLearningObject >> addFromObjectSelector: aSelector

     | method |
     method := OpalCompiler new
                     class: self class;
                     source: (Object sourceCodeAt: aSelector);
     self class addSelector: aSelector withMethod: method.

ATMinimalLearningObject subclass: #ATProtozoid
     instanceVariableNames: 'ff'
     classVariableNames: ''
     package: 'MiniActalk2'

Now it is working but I have the impression that we may have a problem.

When I do
ATProtozoid new instVarAt: 1

I get the following definition for instVarAt:

instVarAt: t1
     self error: 'Decompilation failed'


Object sourceCodeAt: #instVarAt: returns the correct string.

  'instVarAt: index
     "Primitive. Answer a fixed variable in an object. The numbering of 
the variables
      corresponds to the named instance variables, followed by the 
indexed instance
      variables. Fail if the index is not an Integer or is not the index 
of a fixed variable.
      Essential. See Object documentation whatIsAPrimitive."

     <primitive: 173 error: ec>
     self primitiveFailed'

May be the compile is not logging the method body.
But since the compiler is full uncommented this is super nice to learn 
and get empowered.


More information about the Pharo-dev mailing list