Skip to content

Instantly share code, notes, and snippets.

import std.meta : staticIndexOf, AliasSeq, Alias, staticMap, ApplyRight, aliasSeqOf;
import std.traits : isImplicitlyConvertible, Parameters, ParameterDefaults, ParameterIdentifierTuple, isCallable, fullyQualifiedName, isSomeString;
import std.conv : text, to;
import std.range : iota, join;
import std.algorithm.searching : count, find;
import std.array : array, byPair;
/// Helper type for creating named parameters on the form args.foo = 42.
struct args {
static auto opDispatch(string name, T)(T arg) {
@Biotronic
Biotronic / headmutable.d
Last active September 7, 2022 13:36
Head-mutable implementation for D
import std.traits;
import std.typecons : rebindable, Rebindable;
alias HeadMutable(T) = typeof(T.init.headMutable());
alias HeadMutable(T, ConstSource) = HeadMutable!(CopyTypeQualifiers!(ConstSource, T));
auto headMutable(T)(T value) {
static if (isPointer!T) {
// T is a pointer, and decays naturally.
return value;
@Biotronic
Biotronic / head-mutable.md
Last active June 15, 2020 13:48
A Pattern for Head-mutable Structures

A Pattern for Head-mutable Structures

Summary

When Andrei Alexandrescu [introduced][on-iteration] ranges to the [D programming language][D], the gap between built-in and user-defined types (UDTs) narrowed, enabling new abstractions and greater composability. Even today though, UDTs are still second-class citizens in D. One example of this is support for head-mutability - the ability to manipulate a reference without changing the referenced value(s). This document details a pattern that will further narrow the UDT gap, by introducing functions for defining and working with head-mutable user-defined types.

Introduction

[D][D] is not [Kernel][kernel] or [Scheme][scheme] - it has first-class and second-class citizens. Among its first-class citizens are arrays and pointers. One of the benefits these types enjoy is implicit conversion to head-mutable - for instance, const(T[]) is implicitly convertible to const(T)[]. Partly to address this difference, D has many ways to define ho

struct ModelA {
class Animal {
TypeInfo eat() {
return typeid(typeof(this));
}
Food eatsFood() {
return null;
}
mixin fixCast!();
}
@Biotronic
Biotronic / 1 - Innledende post
Last active June 8, 2019 10:02
Global oppvarming, episode 3
Jeg skulle så gjerne visst hva realitetene er!
(Tekst fra dette bildet: https://i.imgur.com/GKtdf5w.png)
KLIMASVINDELEN
1970-årene - Ny istid på vei!
1980-årene - Sur nedbør og skogsdød!
1990-årene - Ozonlaget forsvinner!
2000-årene - Global oppvarming!
@Biotronic
Biotronic / packs.d
Last active March 3, 2019 21:46
Compile-time structs in D
module packs;
import std.meta;
import std.traits;
/*
* Named packs (compile-time structs, or compile-time tuples) in D.
*
* PackedAliasSeq was recently discussed[0] on the D forum. Turns out, an
* implementation already exists in std.meta, called Pack.
* While Pack enables the implementation of some interesting algorithms (StaticZip,
@Biotronic
Biotronic / complexfunctions.d
Last active February 19, 2019 08:18
Missing math functions in std.complex
import std.complex;
import std.math : log, ceil, floor, round, sinh, cosh, trunc, sin, cos, acos, PI, E, copysign, isInfinity, isNaN,
// sgn needs to be renamed as std.math.sgn greedily accepts any type whatsoever and fails spectacularly when used with non-builtins.
fsgn = sgn;
import std.traits : isFloatingPoint;
// Formulas from
// http://scipp.ucsc.edu/~haber/archives/physics116A10/arc_10.pdf
// http://www.suitcaseofdreams.net/Inverse_Functions.htm
// http://www.suitcaseofdreams.net/inverse__hyperbolic_functions.htm
@Biotronic
Biotronic / nullcoalesce.d
Last active August 27, 2018 07:59
Null-coalescing operator in D
struct NullCoalesce {
static auto opBinaryRight(string op : "|", T)(T lhs) {
return NullCoalesceImpl!T(lhs);
}
}
alias NullCoalesce nil;
struct NullCoalesceImpl(T) {
T value;
@Biotronic
Biotronic / customint.d
Created May 21, 2018 15:30
A custom int type supporting various signed number representations as well as different endiannesses
module customint;
import std.traits : Unsigned;
import std.algorithm : among;
import std.conv : to;
/// The representation used for an instance of CustomInt.
/// Details on how these work can be found on https://en.wikipedia.org/wiki/Signed_number_representations
enum Representation
{
@Biotronic
Biotronic / properties.d
Last active March 14, 2018 23:26
Library-only properties in D
struct S {
int n;
mixin property!("field", "get => this.n", "set => this.n = set");
}
unittest {
import std.conv : to;
S s;
s.field = 3;
assert(s.field == 3);