Hi,


> Ladislav wrote:
> > the solution was presented here some time ago. Can repost it.
>
> I must have missed it. Please do so.
>
> Thanks!
>
> Andrew Martin
> Full of curiosity...
> ICQ: 26227169
> http://members.xoom.com/AndrewMartin/
> -><-

I decided to enhance it a bit...

; method creator
meth: func [
    {defines a user method with given spec and body}
    spec [block!]
    body [block!]
] [
    ; use copies of the original spec and body blocks
    spec: copy spec
    body: copy/deep body
    ; find the right place to insert the Self specification
    while [
        all [
            not tail? spec
            any [string? first spec block?first spec]
        ]
    ] [spec: next spec]
    ; insert the Self specification
    insert spec copy/deep [self [object!]]
    func head spec compose/deep [
        do bind/copy [(body)] in self 'self
    ]
]

; method calling dialect:

!: func [
    {method caller}
    [catch]
    message [block!] {
        The structure of a message is as follows:

        method-name
        object
        additional arguments according to the method spec
    }
    /local method-name self position method
] [
    set/any [method-name position] do/next message
    if not word? get/any 'method-name [
        throw make error! {method name is not a word}
    ]
    set/any [self position] do/next position
    if not object? get/any 'self [
        throw make error! {self is not an object}
    ]
    if not method: in get self/class method-name [
        throw make error! {no such method}
    ]
    do compose [(method) (self) (position)]
]

; sample class, feel free to change it as you please:

sample-class: make object! [
    ; a method returning the object type
    type: meth [] [
        class
    ]
    ; a static value - counter
    counter: 0
    ; a counting method
    count: meth [] [
        counter: counter + 1
    ]
]

; sample objects of a sample-class

sample-object1: make object! [
    class: 'sample-class
    number: ! ['count self]
]

sample-object2: make sample-object [
    number: ! ['count self]
]

; sample method calls
! ['type sample-object1]
! ['type sample-object2]
! ['nonsense sample-object1]

Reply via email to