I'm writing a general conversion template function a la:
pure T convert (T, U) (const (U) value);
Sweet, and really handy for template errors because you can tell the user which
number input it is that angered it. The problem is that if you're converting
int to string there's allocations there that doesn't have to be there for many
purposes (also I'm using it for error formatting, which isn't so good if what
I'm telling the user is that there's no memory for allocations). So I thought
I'd make it work like this:
// May call write multiple times for arrays, each time builds onto the array.
void convertInto (T, U, alias write) (const (T) value)
{
write ("You've bespoiled my honour, cur!\n");
}
void convertWith (T, U) (const (U) value, void delegate (T) write)
{
convertInto! (T, U, write) (value);
}
import std.stdio;
void main ()
{
void write (string text) { writef (text); }
convertWith! (string, string) ("Let's you and I spend a few moments alone
without a chaperone, baby.\n", &write);
}
Strangely enough this compiles but it doesn't actually DO anything (everything
works, it's just that convertInto is not called at all); just taking the pure
off fixes it. Obviously it shouldn't work, but what would I do to make an
conditionally-pure function like this without code replication or mixin madness?
It feels like I'm missing some kind of "third way".