Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess object members when using the locals-gathering FUNCTION instead of FUNC
    primarykey
    data
    text
    <p>The low-level primitives in Rebol for functions and closures are FUNC and CLOS. Without explicitly telling the FUNC or CLOS to make something local, then assignments will not be local.</p> <pre><code>x: 10 y: 20 foo: func [/local x] [ x: 304 y: 304 ] foo print [{x is} x {and} {y is} y] </code></pre> <p>This will output:</p> <blockquote> <p>x is 10 and y is 304</p> </blockquote> <p>The higher-level routines FUNCTION and CLOSURE are written as Rebol code in the default library. They scan the body for symbols of category SET-WORD (such as <code>x:</code> and <code>y:</code>). Then they automatically generate an augmented function specification which adds them as /LOCAL:</p> <pre><code>x: 10 y: 20 foo: function [] [ x: 304 y: 304 ] foo print [{x is} x {and} {y is} y] </code></pre> <p>This will output:</p> <blockquote> <p>x is 10 and y is 20</p> </blockquote> <p>That's better almost all of the time, so it's good that these get the prettier names. Yet how can you use FUNCTION as an object member?</p> <pre><code>bar: object [ x: 10 y: 20 foo: function [] [ x: 304 y: 304 c: 12-Dec-2012 d: $0.50 ] ] </code></pre> <p>That won't behave like in other languages where within an object, it's assumed that the members are not hidden by local variables by default. What is someone to do if they want <code>foo</code> to act like a FUNC on any words set in the object, but a FUNCTION for words that are not?</p> <p>The only thing I thought of was to pass <code>self</code> into a variant of the code for FUNCTION, something like:</p> <pre><code>method: func [ me [object!] {always the parameter "self"?} spec [block!] body [block!] ] [ unless find spec: copy/deep spec /local [append spec [ /local ]] body: copy/deep body append spec exclude collect-words/deep/set/ignore body words-of me spec foreach l next find spec /local [ if refinement? l [ break ] insert body to-lit-word l insert body 'unset ] make function! reduce [spec body] ] </code></pre> <p>But then you would have to write <strong>foo: method self [] [...]</strong> which is wordy <em>(assuming this approach is even legitimate)</em>.</p> <p>Is there any trick to get past passing in <code>self</code>, or some other idiom for supporting this desire? Or does everyone just use FUNC as object members?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload