Why I loved D :)
Namespace
rswhite4 at googlemail.com
Mon Oct 7 14:24:21 PDT 2013
On Monday, 7 October 2013 at 21:04:35 UTC, Kozzi wrote:
> In my work we are rewriting some of ours modules from PHP to D.
> And today one of my colleague want to rewrite some of PHP code,
> where he use list statement. I never use this statement in PHP.
> So I do not know if there is a some alternative in D phobos. So
> I try to write my own solution. And it took approximately only
> one minute and that why I ove D.
>
> Because I was able to implement same functionality with same
> syntax quite fast :).
>
> Here is my solution. Yes I know, it is not perfect but it works
> :P.
>
> import std.stdio;
>
> struct list
> {
> void*[] ptrs;
> static list opCall(T...)(auto ref T vars)
> {
>
> list ls;
> foreach(ref var; vars)
> {
> ls.ptrs ~= &var;
> }
> return ls;
> }
>
> void opAssign(T)(T[] values)
> {
> foreach(index, ptr; ptrs)
> {
> *(cast(T*)ptr) = values[index];
> }
> }
> }
>
> void main(string[] args)
> {
> int a, b, c;
> list(a, b, c) = [1,2,3];
> writeln(a);
> writeln(b);
> writeln(c);
>
> }
I would prefer something like this:
----
import std.stdio;
struct List(T...) {
public:
alias Type = T[0];
Type*[] ptrs;
void opAssign(Type[] values) {
foreach (index, ptr; ptrs) {
*ptr = values[index];
}
}
}
List!U list(U = T[0], T...)(auto ref T vars) {
List!U tmpList;
foreach (ref var; vars) {
tmpList.ptrs ~= &var;
}
return tmpList;
}
void main(string[] args) {
int a, b, c;
list(a, b, c) = [1, 2, 3];
writeln(a);
writeln(b);
writeln(c);
}
----
More information about the Digitalmars-d
mailing list