strange template syntax

Philippe Sigaud philippe.sigaud at gmail.com
Sun Apr 11 12:34:56 PDT 2010


On Sun, Apr 11, 2010 at 17:01, Robert Clipsham <robert at octarineparrot.com>wrote:

> When using your method, you have to use:
> ----
> each!(array, typeof(array))((int item) {writefln("%d", item+b)});
> ----
> (I believe this is a bug, dmd should be able to deduce the type here).


OK.  I suppose I'd do:

void each(alias array)(void delegate(ElementType!(typeof(array)) item) dg)
if (isArray!(typeof(array))) {
 foreach(T i; array)
  dg(i);
}

and then:

each!([0,1,2,3])(
    (int i) { writeln(i);}
);




> As for the syntax, you can do this with any function in D:
> ----
> void foo(int a) { writefln( "%d", a ); }
> /// Prints 1
> foo = 1;
> ----
> I didn't realize this worked for free functions, apparently it does. I
> think in newer versions of D2 functions like this will have to be marked
> with @property, I don't think dmd currently enforces this though.
>

Urgh. OK, I didn't think of properties, thanks a lot Robert !
I think it explains some strange errors I have somewhere else, when trying
to assign some value and getting strange unvalid args errors. DMD transforms
my foo = something into foo(something).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100411/4ce8e49b/attachment.html>


More information about the Digitalmars-d-learn mailing list