Skip to content

Instantly share code, notes, and snippets.

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 / 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;
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 / 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 / headmutable.md
Created March 12, 2018 07:57
Implicit Head-mutable Conversion for Temporaries

Implicit Head-mutable Conversion for Temporaries

Field Value
DIP: 1xxx
Review Count: 0
Author: Biotronic

Abstract

This document describes a method for implicitly converting const data structures to head-mutable when types are inferred for temporary variables. This already happens for built-in pointers and arrays, but user-defined types do not enjoy the same benefit. This document argues for a solution where the existing alias this feature is pressed into service to create temporaries with the desired head-mutability when the aliased value has the right type.

@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);
@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 / 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 / Layout.d
Last active March 5, 2018 12:25
Suggested design for Layout!T
module layout;
import std.meta;
import std.traits;
import packs;
/**
* Deconstructs a type into its primitive fields, recursing into sub-types.
*
* Params: