Libmir Archive
mir-coremir-core

mir-core

Base building blocks for the Mir ecosystem — algebraic types (sumtype / tagged union / variant), universal reflection API, and basic math primitives. BetterC and @nogc compatible.

📖 Full API reference → mir-core.libmir.org — every module and symbol of mir-core, regenerated from current source.

mir-core provides the low-level primitives that the rest of the Mir ecosystem depends on. Its most-used feature is mir.algebraic — a type-safe discriminated union (tagged union / sumtype / variant) that compiles in BetterC and @nogc contexts.

GitHub: libmir/mir-core · Old docs: mir-core.libmir.org (now here)

Install

dependency "mir-core" version="~>1.0"

Modules

What's in mir-core

mir.algebraic — tagged unions / sumtype

import mir.algebraic;

alias MyVariant = Variant!(int, double, string);

MyVariant v = 42;
v.match!(
    (int i)    => writeln("int: ", i),
    (double d) => writeln("double: ", d),
    (string s) => writeln("string: ", s),
);

// Type-safe access
if (auto ip = v.peek!int)
    writeln("It's an int: ", *ip);

mir.math.common — math primitives

Basic math functions optimized for use in @nogc nothrow contexts:

import mir.math.common;

double y = fabs(-3.14);     // abs value
double z = sqrt(2.0);       // square root
double w = fmin(a, b);      // min without NaN propagation

mir.conv — conversions

Type conversions that work in BetterC:

import mir.conv : to;

int i = "42".to!int;
string s = 42.to!string;

mir.reflection — universal reflection

Introspect struct fields at compile time:

import mir.reflection;

struct Point { double x; double y; }

// Field names as a tuple of strings
alias names = FieldNames!Point;   // ("x", "y")

// Iterate fields at runtime
auto p = Point(1.0, 2.0);
foreach (i, ref field; p.tupleof)
    writeln(FieldNames!Point[i], " = ", field);

Used internally by mir-ion for automatic serialization.

BetterC usage

All of mir-core works under -betterC:

// No runtime, no GC
import mir.algebraic;

extern(C) void processValue(int tag, void* data) @nogc nothrow
{
    alias V = Variant!(int, float, bool);
    V v = *(cast(V*) data);
    // match works without exceptions or GC
    v.match!(
        (int i)   => { /* handle int */ }(),
        (float f) => { /* handle float */ }(),
        (bool b)  => { /* handle bool */ }(),
    );
}

Relationship to other Mir libraries

mir-core is a dependency of:

  • mir-algorithm — uses algebraic types and math primitives
  • mir-ion — uses reflection for automatic struct serialization
  • mir-random — uses math primitives
  • mir-optim — uses algebraic types for result types

On this page