02-objects/03-reflective-slots.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
9ed574ffdb7253783eb0c0a6333704e8ae66c9df
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

// Reflective slots with runtime-computed names.
//
// `object.name: value` and `object.name = value` need the slot name written in
// the source. createSlot(name, value) and updateSlot(name, value) are the
// ordinary messages for names computed at run time. createSlot never
// overwrites; updateSlot never creates. Neither falls back to the other.

record: {}
fields: [["title", "Protos"], ["year", 2026]]
for(field in fields) {
    record.createSlot(field[0], field[1])
}
print(record.title)                             // Protos
print(record.slotValue("year"))                 // 2026
print(record.slotNames().size())                // 2

// Both answer the exact supplied value.
key: "ti" + "tle"
print(record.updateSlot(key, "Protos 0.4"))     // Protos 0.4
print(record.title)                             // Protos 0.4

// Names need not be identifiers.
record.createSlot("release date", "2026-12-03")
print(record.slotValue("release date"))         // 2026-12-03

// Creating an existing local slot fails and keeps the old value.
print(Error.handle(() => { record.createSlot("year", 1); "created" }, (error) => { "duplicate slot rejected" }))
print(record.year)                              // 2026

// Updating a missing local slot fails and creates nothing.
print(Error.handle(() => { record.updateSlot("missing", 1); "updated" }, (error) => { "missing slot rejected" }))
print(record.hasSlot("missing"))                // false

// Delegation: updateSlot never writes to a parent; createSlot makes a local
// override and leaves the parent unchanged.
base: { color: "red" }
child: base {}
print(Error.handle(() => { child.updateSlot("color", "blue"); "updated" }, (error) => { "inherited-only slot not updated" }))
child.createSlot("color", "blue")
print(child.color)                              // blue
print(base.color)                               // red

// A frozen object rejects both operations.
frozen: { size: 1 }
frozen.freeze()
print(Error.handle(() => { frozen.updateSlot("size", 2); "updated" }, (error) => { "frozen object rejected update" }))