people[name=="Andrew"].friends ~= peter

antonio antonio at abrevia.net
Thu May 4 17:21:11 PDT 2006


As I introduced in http://www.dsource.org/forums/viewtopic.php?t=967 
object relations could be seen as hierarchycal structures.

¿Why not to introduce native syntax to "navigate into"/"Selec from" this 
kind of hierarchies?


ex 1: Add Peter to the friends of people named Andrew and older than 18.

Person peter = ...;
Person[] people = ...;

people[name=="Andrew" && age>18].friends ~= peter;


ex 2: Obtain the array of not married people childs.
ex 2.a: with duplicates:

Person[] modernChilds = people[!married].childs;

ex 2.b: without duplicates:

Person[] modernChilds = Distinct(people[!married].childs);

ex 3: Do something with the married childs of people with friends named 
Andrew
ex 3.a: using foreach (1 by 1 evaluation)

foreach( Person someone; 
people[friends[name=="Andrew"].length!=0].childs[married] ){
   someone.doSomething();
}

ex 3.b: Only 1 stament:

people[friends[name=="Andrew"].length!=0].childs[married].doSomething();

ex 3.c: without duplicates
Distinct(people[friends[name=="Andrew"].length!=0].childs[married].doSomething();

(¿could Distinct be solved with a Template?)
---

These examples could be solved using "D" standard syntax.

ex: (thanks to csauls)

Person[] p = cities[name=="Madrid"].population[age>18 && name=="Peter"];

is equivalent to

Person[] p;
foreach (City x; cities) {
   with (x) {
     if (name == "Madrid") {
       foreach (Person y; population) {
         with (y) {
           if (age > 18 && name == "Peter")
             p ~= y;
         }
       }
     }
   }
}

Basically:

ARRAY[CONDITION].SOMETHING;

could be traslated to:

foreach(T x; ARRAY)
   with(x)
     if( CONDITION ) {
	SOMETHING;
     }

When CONDITION contains sub-ARRAY evaluations, it could be expanded as

foreach(T x; ARRAY)
   with(x) {
     bool b;
     """Expand CONDITION and put result into b""";
     if( b ) {
	DOSOMETHING;
     }
   }

Of course, this "expanding" method doesn't solve all the posibilities...

	people[age>10] = new Person("Foo");

---

The main discussion about this idea was focused in 2 points:
1. "dot" or "not dot":
	people[.married && .age>18]
	vs
	people[married && age>18]

    "not dot" is more "D" syntax compliat (thanks to csaul).

2.Array syntax vs "Template" syntax:
	people[married && age>18]
	vs
	people![married && age>18]

   Personally, I prefer "Array syntax":
	Person p = people[5];
	Person[] p = people[3..5]; // 3..5 is a "condition"
	Person[] p = people[5]; // ¿why not?
	Person[] p = people[married];
	Person[] p = people[age>18 && married];
   	
	
---
In the forum, we told about C# 2.0 similar sintax based on XPath I think 
than this "D" proposal is more powerful.


I have a very poor english... sorry:  be comprensive :-)
Antonio


		



More information about the Digitalmars-d mailing list