Producing nicer template errors in D libraries

Artur Skawina art.08.09 at gmail.com
Tue Apr 10 13:13:29 PDT 2012


On 04/10/12 21:45, H. S. Teoh wrote:
> This produces far more readable error messages when an error happens.
> But it also requires lots of boilerplate static if's for every function
> that currently uses isOutputRange in their signature constraint.
> 
> So here's take #2:
> 
> 	void put(T,R)(R range, T data) if (assertIsOutputRange!R) { ... }

> What do y'all think of this idea?


I had to do this in the gtk bindings, otherwise it was pretty
much impossible to figure out what was wrong just from the compiler
errors, when you eg forgot to mark a callback as extern(C).
So now i have template constraints like this one:

   ulong signal_connect(string name:"activate-link", CB/*:signal_activate_link*/)
         (CB cb, void* data=null, ConnectFlags cf=cast(ConnectFlags)0)
         if (is(typeof(cb)==signal_activate_link)||_ttmm!(CB, signal_activate_link)()) {
      return signal_connect_data!()(&this, cast(char*)"activate-link",
      cast(GObject2.Callback)cb, data, null, cf);
   }

and a helper template:

   // Give the user at least a chance to figure out what's wrong when a template
   // constraint doesn't match - print both the found and the expected types.
   bool _ttmm(T, E)() {
      pragma(msg, "\nExpected: '" ~ E.stringof ~"';\n   found: '" ~ T.stringof ~"'.\n");
      return 0;
   }

static assert wasn't necessary.

artur


More information about the Digitalmars-d mailing list