Tag Archives: D

The amazing template that does nothing

I’ve recently started using a new pattern more often in new D code, namely use of the Identity template.

The Identity template (available in std.traits, although undocumented), is declared as follows:

alias Identity(alias X) = X;

This is a short-form of a template declaration, and is synonymous with the explicit form:

template Identity(alias X)
{
    alias Identity = X;
}

As this is an eponymous template, the instantiation is replaced with X (the only template parameter). Thus, Identity!(anything) is aliased away to anything.

So, what use is a template which does nothing?

I’ve found a number of interesting uses for it: Continue reading

ae.utils.funopt

ae.utils.funopt is a std.getopt enhancer:

import std.stdio, ae.utils.funopt;

int run(bool verbose, string input, string output)
{
	// ...
	return 0;
}

int main(string[] args)
{
	try
		return funopt!run(args);
	catch (Exception e)
	{
		stderr.writeln("Error: ", e.msg);
		return 1;
	}
}

Running program --verbose input.txt output.txt will cause run to be called with the appropriate arguments. Continue reading

Splicing git repositories

Some projects eventually get split up into multiple source repositories for whatever reason. Sometimes, it is useful however to present the project as a single repository – it’s more difficult to examine a project’s development history when it is scattered among several repositories. Specifically, git bisect will not be very helpful if there is strong inter-dependency between the repositories, such as with the reference D programming language implementation. Or you may just not like the layout, which pushes you into using a complicated build process you may not want to use, which was the case for me with the new DerelictOrg repositories. Continue reading

Functional image processing in D

I’ve recently completed an overhaul of the graphics package of my D library. The goals for the overhaul were inspired by D’s std.algorithm and std.range modules:

  • Present everything as small, composable components
  • Avoid implicit copying and prefer lazy evaluation
  • Use templates for efficient code

From its first iteration, all components of the image processing package were templated by the color type. This is not a conventional way to implement graphics libraries – most libraries abstract away the exact color type the image uses behind an OOP interface, or they simply convert all images to a single in-memory pixel format. However, for most cases, this is wasteful and inefficient – usually, the programmer already knows the exact format that an image will be in, with notable exceptions being applications in which the image data comes from user input (e.g. image editors). Instead, this library declares all image types as templates, with the type indicating the image’s color being a template parameter.

I’m rather pleased with the result of this overhaul, so I’d like to share some highlights in this article. Continue reading

Low-overhead components

My personal dream of an ideal programming language is one that allows defining flexible, configurable components that can be coupled together with very little overhead, producing in the end code that, if reverse-engineered, would appear to be hand-written and optimized for the specific task at hand. Preconfigured component configurations / presets would be available for common use, favoring safety and feature-completeness, but for performance-critical cases, the programmer could break them down and strip out unneeded features to reduce overhead, or customize by injecting their own components into the mix. Continue reading

DHCP test client

While trying to set up my home network, I was dismayed that there was no simple way to test the DHCP server. Snooping packets is limited to examining existing traffic.

DHCP test tools exist (DHCPing and dhquery), however both are outdated and don’t work with the latest versions of their requirements, and both won’t work on Windows.

I’ve written a simple DHCP “client” which can receive and decode broadcasted DHCP replies, as well as send out DHCP “discover” packets. The tool is cross-platform, and should work on Windows and major POSIX systems.

Source, Windows binaries.

data.d – unmanaged memory wrapper for D

I have written a module containing classes to manage raw data in external memory. It provides semantics similar to built-in void[] arrays, but has the following advantages:

  • Faster allocation and deallocation, since memory is requested from the OS directly as whole pages
  • Greatly reduced chance of memory leaks due to stray pointers
  • Overall improved GC performance due to reduced size of managed heap
  • Memory is immediately returned to the OS when data is deallocated

Source and more info here: http://github.com/CyberShadow/data.d