Coderive Programming Language: Designed to be Used for 2026 Onwards

Viral Policies in Coderive: Interfaces That Actually Inherit

Forget traditional interfaces – meet policies that automatically propagate through your inheritance tree

Introduction

What if interfaces weren’t just contracts you implement, but contracts that automatically apply to all your descendants? Welcome to Viral Policies – Coderive’s revolutionary approach to interface design that solves real-world inheritance problems.

The Problem with Traditional Interfaces

In most languages, when a parent class implements an interface, children don’t inherit that obligation:

// Traditional languages
interface Accept { void accept(Visitor v); }
abstract class Node implements Accept {
    // must implement accept()
}
class BinaryNode extends Node {
    // OOPS! Forgot to implement accept() - but compiler doesn't complain!
}

This leads to runtime errors, fragile hierarchies, and the “why doesn’t this work?” debugging sessions we all hate.

The Coderive Solution: Viral Policies

In Coderive v0.6.0, policies are viral by default:

// Define a policy (like an interface)
policy Accept {
    accept(visitor: Visitor)
}

// Parent implements it
share Node with Accept {
    // Node must implement accept()
}

// Child automatically inherits the obligation
share BinaryNode is Node {
    // COMPILE ERROR: Must implement accept() from ancestor policy!
    // The compiler enforces this at parse time
}

How Viral Policies Work

The magic happens in our new DeclarationParser:

private boolean isMethodRequiredFromAncestorPolicy(
    String methodName, TypeNode currentClass, ProgramNode program) {
    // Check if any ancestor implements a policy requiring this method
    List<String> ancestorPolicies = getAncestorPolicies(currentClass, program);
    // Recursively check policy composition
    return requiresMethodRecursive(ancestorPolicies, methodName);
}

Real Example: Visitor Pattern Done Right

// Define visiting policies
policy Accept {
    accept(visitor: Visitor)
}

policy Visitable {
    getChildren(): []Node
}

// Base node requires both
share Node with Accept, Visitable {
    policy accept(visitor: Visitor) {
        visitor.visit(this)
    }

    policy getChildren() ~> []
}

// All descendants must implement both
share BinaryNode is Node {
    left: Node
    right: Node

    // Explicit policy implementation (compiler-enforced)
    policy getChildren() ~> [this.left, this.right]

    // Inherits accept() from Node automatically
}

Composition Over Inheritance (But Better!)

Policies support composition, creating reusable behavior bundles:

// Compose policies
policy Serializable with Accept, ToString {
    // Combines requirements from both
}

// Use composed policy
share Document with Serializable {
    // Must implement accept() AND toString()
}

The Compiler’s Role

Our enhanced compiler performs four-stage policy validation:

  1. Parse-time validation: Immediate feedback on missing implementations
  2. Inheritance chain analysis: Automatic detection of viral requirements
  3. Composition cycle detection: Prevents infinite policy loops
  4. Type compatibility checking: Ensures method signatures match

Benefits for Developers

  1. No More “Forgotten Implementation” Bugs: The compiler catches missing implementations immediately
  2. Self-Documenting Code: The with clause explicitly declares capabilities
  3. Refactoring Safety: Change a base policy, see all required updates instantly
  4. Team Scaling: New team members can’t accidentally break the interface contract

Advanced Feature: Conditional Policy Application

// Only apply policy to certain descendants
policy Debuggable with Accept when isDebug {
    debugInfo(): Text
}

// Compile-time flag controls whether policy applies
share ProductionNode is Node {
    // No debugInfo() required in production
}

Performance Considerations

Unlike runtime interface checks, viral policies are validated at compile time:

· Zero runtime overhead for policy checking
· Incremental validation during development
· Cached policy resolution across files

The Philosophy Behind Viral Policies

We believe interfaces should be promises that propagate. If a parent promises a capability, all children should uphold that promise. This eliminates entire categories of bugs and makes class hierarchies more predictable and maintainable.

Try It Yourself

unit demo.viralpolicies

// Your first viral policy
policy Greetable {
    greet(): Text
}

share Person with Greetable {
    name: Text

    policy greet() ~> "Hello, I'm " + this.name
}

share Employee is Person {
    title: Text

    // Try removing this - compiler will catch it!
    policy greet() ~> super.greet() + ", " + this.title
}

Call to Action: Experiment with viral policies in your new Coderive project. You’ll never want to go back to traditional interfaces again.

Repo link: Click here

Leave a Reply