Tinderbox Forum

Inheritance, using "lastChild" vs child[]

Working my way through Tinderbox and experimenting with inheritance I have problem with code, that in my mind should yield identical result, but they don’t.

The task is to add children to a mother note. The color of children notes with even child number should be colored “red”, the odd numbered should be colored “bright green”.

I create a mother note and a number of notes that are to be moved into the mother container one by one.

This code is assigned as a rule to the mother note;

if(mod($ChildCount,2))
	{$Color(lastChild)="red";}
else
	{$Color(lastChild)="bright green";};

This works as expected and the color takes on red, bright green, red, bright green…

I tested another way to accomplish the same thing

if(mod($ChildCount,2))
	{$Color(child[$ChildCount-1])="red";}
else
	{$Color(child[$ChildCount-1])="bright green";};

This also works, except that after the child is added and assigned its proper color attribute, all of other child color-attribute also changes to the color of this last added child.

This is an interpreter bug; it will be fixed in the next release.

Another way to approach this would be an OnAdd action:

if(mod($SiblingOrder,2)) {$Color="red";} else {$Color="green"}

This has the advantage of only needing to run once, but the disadvantage of getting the wrong color if we add notes in the middle of the list.

Alternatively:

if($Color(prevSibling)=="red") {$Color="green"} else {$Color="red"}

Ok,thanks