SuperC documentation

⚠️ This is a PROPOSAL DRAFT. Generics are currently not implemented. Syntax may change

Generics programming

The compiler automatically duplicates and generates at compile-time all the type-specific implementations for the template region you defined as a generic implementation template.

Define template region

When Not to Use Generics

Generics programming is meant to be a last resource tool when there’s no other option; or you want a more type safe, but more verbose solution.

Many cases can be covered with one of the following before using generics:

  Macros Overloads Generics
Compile-time âś… âś… âś…
Type safety ❌ ✅ ✅
Flexible types ✅ ❌ ✅
Code duplication ❌ ❌ ⚠️
Symbol control ❎* ✅ ❌

(*) macros are preprocessed so they don’t have a symbol, but that’s not bad.

Symbol mangle

When duplicating the codebase, the compiler automatically mangles the symbols of the functions and methods to avoid name collisions.

Warning: Do not try to assign a custom symbol to a function or method inside a generic region, the symbol will be duplicated. The compiler automatically generates unique, mangled symbols for every type‑specific implementation.
Unfortunately, this is a natural limitation of code duplication. For low-level and ffi scenarios, where symbol stability is required, generics might not be the right solution.

#include <stdio.h>

#pragma generic T: auto
struct Point[T] {
  T x, y;
};

T math::sum[T](T a, T b);

void (Point[T] *this) add(T other);

// math::sum[int]   => "math$$sum.i"
// math::sum[float] => "math$$sum.f"

// point_i.add(1)   => "add$PS7Point.ii"
// point_f.add(1.0) => "add$PS7Point.ff"

// math::sum[struct Point[int]]   => "math$$sum.S7Point.i"
// math::sum[struct Point[float]] => "math$$sum.S7Point.f"
#pragma endgeneric

Examples


Previous

(DRAFT) Defer edge cases
Playground