www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why I loved D :)

reply "Kozzi" <kozzi11 gmail.com> writes:
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);

}
Oct 07 2013
next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
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);

 }
That is awesome. I love this features in PHP and miss it in many other languages. :)
Oct 07 2013
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
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); } ----
Oct 07 2013
prev sibling next sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 10/07/13 23:04, 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);
 
 }
Neat. But dangerous. You'll want type safety (ie using the same 'T' everywhere) and 'ref' instead of 'auto ref' (the latter will accept rvalues, so you could be taking an address of a local variable and escaping it). Let me try, with a slightly safer version: void list(A...)(typeof([A]) a) property { foreach (I, ref _; A) A[I] = a[I]; } void main(string[] args) { int a, b, c; list!(a, b, c) = [1, 2, 3]; import std.stdio; writeln(a); writeln(b); writeln(c); } SCNR. We need an IODCC. :^) artur
Oct 07 2013
parent reply "Meta" <jared771 gmail.com> writes:
On Monday, 7 October 2013 at 21:56:21 UTC, Artur Skawina wrote:
 Neat. But dangerous. You'll want type safety (ie using the same 
 'T'
 everywhere) and 'ref' instead of 'auto ref' (the latter will 
 accept
 rvalues, so you could be taking an address of a local variable 
 and
 escaping it).

 Let me try, with a slightly safer version:

    void list(A...)(typeof([A]) a)  property { foreach (I, ref 
 _;  A) A[I] = a[I]; }

    void main(string[] args) {
        int a, b, c;

        list!(a, b, c) = [1, 2, 3];

        import std.stdio;
        writeln(a);
        writeln(b);
        writeln(c);
    }

 SCNR. We need an IODCC. :^)

 artur
We are so close to a destructuring syntax it hurts. Is there any way to insert a, b and c into the current scope automagically?
Oct 07 2013
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 10/8/13, Meta <jared771 gmail.com> wrote:
 Is there any
 way to insert a, b and c into the current scope automagically?
Not without a mixin statement or a mixin expression.
Oct 07 2013
parent "Meta" <jared771 gmail.com> writes:
On Monday, 7 October 2013 at 23:22:04 UTC, Andrej Mitrovic wrote:
 On 10/8/13, Meta <jared771 gmail.com> wrote:
 Is there any way to insert a, b and c into the current scope 
 automagically?
Not without a mixin statement or a mixin expression.
That's something along the lines of what I was thinking. Do you have a specific example you're thinking of, or just a general "yeah, it can probably be done"?
Oct 07 2013
prev sibling next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 10/7/13 4:08 PM, Meta wrote:
 We are so close to a destructuring syntax it hurts. Is there any way to
 insert a, b and c into the current scope automagically?
No, we need to add syntax for that. Once it becomes clear that's all we need for glommable tuples, we'll add it. Andrei
Oct 07 2013
prev sibling parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On Tue, 08 Oct 2013 01:08:42 +0200
"Meta" <jared771 gmail.com> wrote:
 
 We are so close to a destructuring syntax it hurts. Is there any 
 way to insert a, b and c into the current scope automagically?
with() <g>
Oct 07 2013
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 10/7/13, Artur Skawina <art.08.09 gmail.com> wrote:
 list(A...)(typeof([A]) a)
That right there is something I would have never thought of. Pretty cool that it's allowed.
Oct 07 2013
prev sibling parent reply "Brad Anderson" <eco gnuk.net> writes:
Nice.

C++'s Boost uses the tuple library to accomplish this:

int i; char c; double d;
tie(i, c, d) = make_tuple(1, 'a', 5.5);

Phobos don't have ref item tuples though so it's not quite so 
simple to do it using this approach in D unfortunately.
Oct 07 2013
parent "Szymon Gatner" <noemail gmail.com> writes:
On Monday, 7 October 2013 at 23:34:35 UTC, Brad Anderson wrote:
 Nice.

 C++'s Boost uses the tuple library to accomplish this:

 int i; char c; double d;
 tie(i, c, d) = make_tuple(1, 'a', 5.5);
It is actually a standard now ;)
Oct 08 2013