Skip to content

Instantly share code, notes, and snippets.

@mkmik
mkmik / -
Created October 15, 2014 13:43
test
@mkmik
mkmik / -
Created October 15, 2014 14:06
/*
* This is dummy port of the Go option pattern to C.
*
* The option pattern is described somewhat obscurely in
* http://commandcenter.blogspot.nl/2014/01/self-referential-functions-and-design.html
*
* In Go it already suffers from some boilerplate explosion, but it's somewhat manageable.
* Here it's even worse, so I'm afraid this is just to be used for educational purposes.
*
* To be fair, it should be possible to throw even some more preprocessor magic to
@mkmik
mkmik / -
Created October 15, 2014 14:10
/*
* This is dummy port of the Go option pattern to C.
*
* The option pattern is described somewhat obscurely in
* http://commandcenter.blogspot.nl/2014/01/self-referential-functions-and-design.html
*
* TL;DR: provide a way for functions to accept optional arguments with the following properties:
*
* 1) backward compatible signature for functions that were born with no thought for options
* 2) allow defaults that are not the zero value of the option struct member value.
@mkmik
mkmik / args.c
Last active August 29, 2015 14:07
/*
* This a is dummy port of the Go option pattern to C.
*
* The option pattern is described somewhat obscurely in
* http://commandcenter.blogspot.nl/2014/01/self-referential-functions-and-design.html
*
* TL;DR: provide a way for functions to accept optional arguments with the following properties:
*
* 1) backward compatible signature for functions that were born with no thought for options
* 2) allow defaults that are not the zero value of the option struct member value.
/*
* Dummy port of the Amiga tag based extensible api. See:
*
* http://amigadev.elowar.com/read/ADCD_2.1/Includes_and_Autodocs_3._guide/node03D6.html
*
*
* note that the amiga api relied on the fact that the calling convention was
* very simple so the vararg stub could just take the pointer to the last argument,
* and get an array of TagItem records.
*
/*
* Dummy port of the Amiga tag based extensible api. See:
*
* http://amigadev.elowar.com/read/ADCD_2.1/Includes_and_Autodocs_3._guide/node03D6.html
*
*
* note that the amiga api relied on the fact that the calling convention was
* very simple so the vararg stub could just take the pointer to the last argument,
* and get an array of TagItem records.
*
/*
* Quicky and dirty optional arguments. Requires (ANSI) C99.
*
* Good:
* 1. Allows to convert any existing function to a optarg function.
* 2. Typed parameters.
*
* So so:
* 3. Simple defaults are possible but might result in compiler
* warnings that can be suppressed but ...
/*
* Quicky and dirty optional arguments. Requires (ANSI) C99.
*
* Good:
* 1. Allows to convert any existing function to a optarg function.
* 2. Typed parameters.
* 3. Users can just use a real option struct.
*
* So so:
* 4. Simple defaults are possible but might result in compiler
ns_send("foo", "bar");
ns_send("foo", "bar", opts);
/*
* Quick and dirty optional option struct via function overload via preprocessor trick.
*
* requires C99 and gnu extension, for ", ## __VA_ARGS"
*/
#include <stdio.h>
///////// Magic library