REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Objects: Cloning Objects

Pending Revision

This document was written for R2 and has yet to be revised for R3.

When you use a parent object to make a new object, the parent object is cloned rather than inherited. This means that if the parent object is modified, it has no effect on the child object.

As an example, the following code creates a bank account object, whose variables are blank:

bank-account: make object! [
    first-name:
    last-name:
    account:
    balance: none
]

To use the new object, values can be provided to create an account for a customer:

luke: make bank-account [
    first-name: "Luke"
    last-name: "Lakeswimmer"
    account: 89431
    balance: $1204.52
]

Since new accounts are made on a regular basis, it helps to use a function and some global variables to create them:

last-account: 89431
bank-bonus: $10.00

make-account: func [
    "Returns a new account object"
    f-name [string!] "First name"
    l-name [string!] "Last name"
    start-balance [money!] "Starting balance"
][
    last-account: last-account + 1
    make bank-account [
        first-name: f-name
        last-name: l-name
        account: last-account
        balance: start-balance + bank-bonus
    ]
]

Now a new account object for Fred would only require:

fred: make-account "Fred" "Smith" $500.00


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