What Are Effects?
Effects are Fe’s system for making dependencies explicit. They declare what capabilities a function needs (what external resources it can access) and the compiler enforces these declarations.
The Problem with Hidden State
Section titled “The Problem with Hidden State”In many languages, functions can access global state, modify storage, or call external services without any indication in their signature. This leads to:
- Hidden dependencies: You can’t tell what a function does just by looking at its signature
- Surprising behavior: Functions may have side effects you didn’t expect
- Testing difficulties: Mocking hidden dependencies is awkward
- Security risks: In smart contracts, hidden state access can lead to vulnerabilities
// In languages without effects, this signature tells you nothing// about what resources the function accessesfn transfer(from: u256, to: u256, amount: u256) {}Effects as Explicit Capabilities
Section titled “Effects as Explicit Capabilities”Fe’s effect system requires functions to declare their capabilities upfront using the uses clause:
fn transfer(from: u256, to: u256, amount: u256) uses (mut balances: Balances) { // This function can only access Balances // and can modify it (mut)}Now the function signature tells you exactly what it can do:
- It needs access to
Balances - It will modify
Balances(indicated bymut) - It cannot access anything else
A Simple Example
Section titled “A Simple Example”Here’s how effects work in practice:
// Define an effect typepub struct Counter { pub value: u256,}
// This function requires the Counter effectfn increment() uses (mut counter: Counter) { counter.value = counter.value + 1}
// This function requires read-only accessfn get_count() -> u256 uses (counter: Counter) { counter.value}
// Caller must provide the effectfn main() { let mut counter = Counter { value: 0 }
// Provide the effect with 'with' with (Counter = counter) { increment() increment() let count = get_count() // count is 2 }}Key Concepts
Section titled “Key Concepts”| Concept | Description |
|---|---|
| Effect | A capability that a function requires to execute |
uses clause | Declares what effects a function needs |
mut | Indicates the function will modify the effect |
with | Provides an effect to a scope |
What Effects Enable
Section titled “What Effects Enable”The effect system provides:
- Explicit dependencies: Function signatures tell you everything
- Compiler enforcement: Missing or incorrect effects are compile errors
- Scoped access: Effects are only available where explicitly provided
- Controlled mutation: Distinguish between reading and modifying
The following sections cover how to declare effects, use mutability, and understand how effects propagate through your code.