REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: construct

construct  block  /with  object  /only

Creates an object with scant (safe) evaluation.

Arguments:

block [block! string! binary!] - Specification

Refinements:

/with - Default object

object [object!]

/only - Values are kept as-is

See also:

make   context  

Description

This function creates new objects but without evaluating the object's specification (as is done in the make and context functions).

When you construct an object, only literal types are accepted. Functional evaluation is not performed. This allows your code to directly import objects (such as those sent from unsafe external sources such as email, cgi, etc.) without concern that they may include "hidden" side effects using executable code.

construct is used in the same way as the context function:

obj: construct [
    name: "Fred"
    age: 27
    city: "Ukiah"
]
probe obj
make object! [
    name: "Fred"
    age: 27
    city: "Ukiah"
]

But, very limited evaluation takes place. That means object specifications like:

obj: construct [
    name: uppercase "Fred"
    age: 20 + 7
    time: now
]
probe obj
make object! [
    name: 'uppercase
    age: 20
    time: 'now
]

do not produce evaluated results.

Except with the /only refinement, the construct function does perform evaluation on the words true, on, yes, false, off, no and none to produce their expected values. Literal words and paths will also be evaluated to produce their respective words and paths. For example:

obj: construct [
    a: yes
    b: none
    c: 'word
]
probe obj
make object! [
    a: true
    b: none
    c: word
]
type? obj/a
logic!
type? obj/c
word!

The construct function is useful for importing external objects, such as preference settings from a file, CGI query responses, encoded email, etc.

To provide a template object that contains default variable values (similar to make), use the /with refinement. The example below would use an existing object called standard-prefs as the template.

prefs: construct/with load %prefs.r standard-prefs


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin