Unittests, templates
Bill Baxter
wbaxter at gmail.com
Thu Oct 30 20:54:55 PDT 2008
On Fri, Oct 31, 2008 at 11:57 AM, bearophile <bearophileHUGS at lycos.com> wrote:
> This is a question regarding templates. Let's say I have a simple struct template that is specified by an integer:
>
> struct S(int N) {
> int[N] a;
> }
>
> Now I'd like to write a function that takes a S, and for example prints the integers of S.a
> separated by a space (I don't care of the extra space at the end).
>
> This is easy to to in Haskell because it has type classes, but it's less easy to do in D (I don't know if type classes can be introduced into the D type system):
> http://en.wikipedia.org/wiki/Type_class
>
> This is a simple solution, but it compiles only if N == 2, so I'd like something more general:
>
> void printS1(S!(2) s) {
> foreach (el; s.a)
> put(el, " ");
> putr();
> }
>
> This is general, a function template, but it requires you to specify N:
>
> void printS2(int N)(S!(N) s) {
> foreach (el; s.a)
> put(el, " ");
> putr();
> }
>
> You have to call it for example like this:
> printS2!(s.a.length)(s);
>
I think this one should work. IFTI should be able to tease apart S to
match the int. If it doesn't work, it seems like a bug to me.
Actually I seem to remember a bug like this in the DB, but I couldn't
find it just now. I was also thinking it was marked fixed.
--bb
More information about the Digitalmars-d-learn
mailing list