This repository serves as a template for creating external shards in the Shards programming language. It demonstrates the basic structure and best practices for implementing external functionality.
.
├── CMakeLists.txt # Build configuration
├── calculator.cpp # Example shard implementation
└── README.md # This file
- Defines project configuration and build settings
- Sets up necessary include paths
- Configures the shared library output
- Handles platform-specific settings
- Contains the actual shard implementations
- Demonstrates how to create operations with:
- Input/output type definitions
- Parameter handling
- State management
- Resource initialization and cleanup
- Error handling
-
Setup Project Structure
- Copy this template
- Rename the project in
CMakeLists.txt
- Create your implementation file
-
Define Your Operations
- Create a namespace for your shard
- Implement structs for each operation
- Define input/output types
- Add parameters if needed
- Implement the
activate
method
-
Register Your Shards
- Use
REGISTER_SHARD
in theregisterExternalShards
function - Follow the naming convention: "Namespace.Operation"
- Use
The template includes a simple calculator example with two operations:
-
Calculator.Add
- Accumulates input values
- Demonstrates basic operation structure
- Shows parameter handling
-
Calculator.Memory
- Implements memory operations (store/recall/clear)
- Shows more complex state management
- Demonstrates parameter-based behavior
mkdir build
cd build
cmake ..
make
-
Type Safety
- Always check input types
- Throw appropriate errors for invalid inputs
- Use clear error messages
-
Resource Management
- Initialize resources in
warmup
- Clean up in
cleanup
- Handle memory carefully
- Initialize resources in
-
Error Handling
- Use
ActivationError
for runtime errors - Provide clear error messages
- Validate inputs and parameters
- Use
-
Documentation
- Comment your code
- Provide clear parameter descriptions
- Document any special requirements
To use your shard in a Shards project:
- Build the shared library
- Create an
externals
folder in the same directory as your Shards script - Place the built library in the
externals
folder - The Shards runtime will automatically load the external shard
Example usage in Shards:
# Using the calculator operations
Calculator.Add(5) # Add 5 to accumulator
Calculator.Memory("store") # Store current value
Calculator.Memory("recall") # Recall stored value
Project structure example:
your_project/
├── your_script.shs # Your Shards script
└── externals/ # External shards folder
└── calculator # The built library