soma.cell

Custom Properties

Define custom properties that extend the memory system. The language grows — no compiler changes needed.

Example 096 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell UserStore declares 2 signals in its face: get(id: String) -> String and put(id: String, name: String) -> String; keeps 2 memory slots: users (Map<String, String>, replicated and encrypted) and sessions (Map<String, String>, cached and ephemeral). Handlers: get (line 49) and put (line 53).

Generated from soma describe on this file.

The source

096_custom_property.cell

// ============================================================================
// 096 — Custom Properties
// ============================================================================
// Define custom properties that extend the memory system.
// The language grows — no compiler changes needed.
//
// Run:   soma check 096_custom_property.cell
// Expect: All checks passed.

// Define a new property: "replicated"
cell property replicated {
    face {
        // Parameters for the property
        given copies: Int
        promise "data is replicated across nodes"
    }
    rules {
        // This property implies persistent and consistent
        implies [persistent, consistent]
        // It cannot be used with ephemeral or local
        contradicts [local, ephemeral]
    }
}

// Define another: "cached"
cell property cached {
    face {
        given ttl: Int
        promise "reads served from cache when possible"
    }
    rules {}
}

// Use the custom properties in a regular cell
cell UserStore {
    face {
        signal get(id: String) -> String
        signal put(id: String, name: String) -> String
    }

    memory {
        // Custom properties used alongside built-in ones
        users: Map<String, String> [replicated(3), encrypted]

        // Cached sessions with TTL
        sessions: Map<String, String> [cached(300), ephemeral]
    }

    on get(id: String) {
        return users.get(id)
    }

    on put(id: String, name: String) {
        users.set(id, name)
        return name
    }
}

What the toolchain says

$ soma check 096_custom_property.cell
note: 'replicated' on 'users' implies 'consistent' (added automatically)
  --> 096_custom_property.cell:43:9
   |
43 |         users: Map<String, String> [replicated(3), encrypted]
   |         ^
note: 'replicated' on 'users' implies 'persistent' (added automatically)
  --> 096_custom_property.cell:43:9
   |
43 |         users: Map<String, String> [replicated(3), encrypted]
   |         ^
✓ All checks passed (2 notes above).