On 9 February 2018 at 00:45, Ben Coman <b...@openinworld.com> wrote:
> On 8 February 2018 at 16:22, Stephane Ducasse <stepharo.s...@gmail.com>
wrote:
>> Hi denis
>>
>> - I would like to be able to define a method even in the class pane
>> and that the system detects it automatically.
>
> It would be nice to do it from anywhere including Playground with
> something like...
> Classname >> #methodname >> [ method source here ]
>
> So I just tired a quick experiment...
>
>    Object subclass: #SomeClass
>         instanceVariableNames: ''
>         classVariableNames: ''
>         package: 'MethodBuilderExperiment'
>
> On SomeClass defined method...
>     existingMethod
>         ^1
>
> On CompiledMethod defined method...
>     >> blockClosure
>         | newSource |
>         newSource := self methodNode selector, String crlf,
>             blockClosure sourceNode newSource allButFirst allButLast.
>         self methodClass compile: newSource
>
>
> Now each of the following lines can be evaluated...
>
> SomeClass existingMethod "==> 1"
> SomeClass >> #existingMethod >> [ ^42].
> SomeClass existingMethod "==> 42"

That should have been.

SomeClass new existingMethod "==> 1"
SomeClass >> #existingMethod >> [ ^42].
SomeClass new existingMethod "==> 42"

>
>
>
> And another experiment...
>
>     Object subclass: #MethodBuilder
>         instanceVariableNames: 'methodClass methodSelector'
>         classVariableNames: ''
>         package: 'MethodBuilderExperiment'
>
> On MethodBuilder defined method...
>     class: aClass selector: aSelector
>         methodClass := aClass.
>         methodSelector := aSelector.
>
> On MethodBuilder defined method...
>     >> blockClosure
>         |newSource|
>         newSource := methodSelector, String crlf,
>             blockClosure sourceNode newSource allButFirst allButLast.
>         methodClass compile: newSource
>
> On Behaviour re-defined method...
>     compiledMethodAt: selector
>         ^ self methodDict at: selector ifAbsent:
>                      [ MethodBuilder new class: self selector: selector ]
>
>
> Now each of the following lines can be evaluated...
>
> Someclass noMethod "==> error... Instance of SomeClass class did not
> understand #noMethod"
> SomeClass >> #noMethod >> [ ^42 ].
> Someclass noMethod "==> 42"

And should have been...
Someclass new noMethod "==> error... Instance of SomeClass class did not
understand #noMethod"
SomeClass >> #noMethod >> [ ^42 ].
Someclass new noMethod "==> 42"


I see that is a bit problematic for keyword messages, but with a tweak...

On MethodBuilder defined...
   >> blockClosure
|newSource|
methodSelector isArray
ifFalse: [ newSource := methodSelector ]
ifTrue: [
newSource := ''.
methodSelector do: [ :token | newSource := newSource , token asString, ' ' ]
].
newSource := newSource, String crlf,
blockClosure sourceNode newSource allButFirst allButLast.
methodClass compile: newSource


now the following can be evaluated...

SomeClass >>#(echo: anObject) >> [ ^anObject ].
SomeClass new echo: 'doyoulike'.   "==> doyoulike"


Another form to define a method might be...
SomeClass >> [ echo: anObject
    ^anObject ]

I think that looks better, but would require a loosening of block syntax.

cheers -ben

>> - Then we are starting to reverse engineering the ecompletion system
>> and I'm learning it.
>> I would like to know if Calypso can communicate with ecompletion the
>> fact that we are defining a method. Then I have to check how the
>> system can know that we are typing method argument when defining the
>> signature but I think that this is not related to calypso.
>>
>> Stef
>>

Reply via email to