Hi, Rebols,

Something like:

!: func [
    object [object!]
    'method [word!]
    /local mth
] [
    ; let's skip error checking for clarity
    mth: get in object/methods method
    bind second :mth in object 'self
    return :mth
]

;text skipped....

a: make object! [
    num: 1
    methods: make object! [
        sumnum: func [other number] [
            switch number [
                1 [self/num]
                2 [ (do ! other sumnum self 1) + (do ! self sumnum other
1) ]
                ]
            ]
        ]
    ]

Actually it works exactly as I would expect it to. What did you expect your
expression

>[ (do ! other sumnum self 1) + (do ! self sumnum other 1) ]

would do? Could you explain?

TIA,

Elan


Ladislav>

Some results:

>> b: make a [num: num + 1]
>> do ! a sumnum b 1
== 1
>> do ! b sumnum a 1
== 2
>> do ! b sumnum a 2
== 2
>> do ! a sumnum b 2
== 4

I think its' OK for you to expect it. No problemo!

I would prefer:

;Function to create methods, adds the hidden SELF argument
makemethod: func [args body] [
    func append copy [self] args body
    ]

;Function to call methods
!: func [self [object!]  'method [word!] param [block!]] [
    do append reduce [in self/methods method self] param
    ]

;end of %methods.r

;now the example:
a: make object! [
    num: 1
    methods: make object! [
        sumnum: makemethod [other number] [
            switch number [
                1 [self/num]
                2 [ (! other sumnum [self 1]) + (! self sumnum [other 1])]
                ]
            ]
        ]
    ]
b: make a [num: num + 1]

>> ! a sumnum [b 1]
== 1
>> ! b sumnum [a 1]
== 2
>> ! a sumnum [b 2]
== 3
>> ! b sumnum [a 2]
== 3

The choice is your's,

but my vote is: (guess what?)

Ladislav

Reply via email to