Skip to content

Instantly share code, notes, and snippets.

@giesse
Created August 2, 2018 03:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giesse/cc603fcbcdbcf771c5cce242a9e9fd29 to your computer and use it in GitHub Desktop.
Save giesse/cc603fcbcdbcf771c5cce242a9e9fd29 to your computer and use it in GitHub Desktop.
Red []
export: func [
"Export a value to the global context"
'word [set-word!]
value
/to ctx [any-word! any-object! function!]
] [
set word :value
unless :ctx [ctx: system/words]
set bind word ctx :value
]
@giesse
Copy link
Author

giesse commented Aug 2, 2018

Examples:

>> context [
[    a: 1
[    b: 2
[    export f: func [c] [a + b + c]
[    ]
== make object! [
    a: 1
    b: 2
    f: func [c][a + b + c]
]
>> f 2
== 5
>> context [
[    other-ctx: context [
[    f: none
[    ]
[    a: 1
[    b: 2
[    export/to f: func [c] [a + b + c] other-ctx
[    ]
== make object! [
    other-ctx: make object! [
        f: func [c][a + b + c]
    ]
    a: 1
    b: 2
    f: func [c][a + b + c]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment