Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page
  • Paths
  • Attributes
  • Private Attribute
  • Read Only Attribute

Was this helpful?

  1. Common Concepts

Fields

An overview of Stof fields.

Fields are the primary way to define data with Stof (but not the only way).

field: 42; // A field named "field" with the value 42 (int)

Paths

Just like objects and functions, fields are organized in objects and are referenced via paths. See the Objects page for more details on paths and how they work.

// referenced via "root.field" from anywhere in the document
// referenced from the "root" object as "self.field"
field: 'hello'

child: {
    // referenced via "root.child.field" from anywhere in the document
    // referenced via "self.child.field" from the "root" object
    // referenced via "self.field" from this "root.child" object
    field: 42
}

Attributes

Just like functions, fields can have attributes. Currently, however, there are not many field attributes that Stof pays attention to. They mostly benefit anyone using Stof to interpret fields in different ways.

Private Attribute

Fields can have a private attribute, making them only visible and operable in the object(s) in which they are attached.

child: {
    #[private]
    field: 42

    #[test]
    fn can_access() {
        assertEq(self.field, 42);

        self.field = 'hello';
        assertEq(self.field, 'hello');
    }

    grandchild: {
        #[test]
        fn cannot_access() {
            assertEq(super.field, null);
        }
    }
}

#[test]
fn cannot_access() {
    assertEq(self.child.field, null);
}

Read Only Attribute

Fields with the "readonly" attribute cannot be modified by functions, only read.

#[readonly]
field: 42

#[test]
fn cannot_modify() {
    assertEq(self.field, 42);

    self.field = 'hello'; // Field cannot be written to
    assertEq(self.field, 42);
}
PreviousFunctionsNextObject Types

Last updated 3 months ago

Was this helpful?