Bug in GetItemAtIndex inside Ternary Operator

I noticed a weird situation lately when I tried to extend/reduce a list to a fixed length. If a list is shorter than 10 items, I wanted to extend it with dashes, if it is longer than 10, I want to reduce it. As far as I know there are no nodes that do this kind of action, so I came up with some code. I use a Ternary Operator, and as long as the list is longer than the current number from the range, it reads the item at index, else it produces a dash.

The result is correct:

But I receive a warning that the GetItemAtIndex tried to get an item outside the bounds.

What is wrong here? It runs the operator 10 times (because of the range 0..9), so after the 3rd value it should no longer evaluate the List.GetItemAtIndex.

Strange thing is that it sometimes work. If I create a new graph and only add these nodes, it works (not always though). When I have saved it, it mostly produce this error.

Im going to take a guess here and assume tenerary still evaluates both outcomes even if not returned.

An alternative could be achieved using a mixture of chop, repeated item and join i think to achieve the desired outcome without ternary evaluation.

A little cutting and pasting can do it

prefixList = List.TakeItems(inList, listLen);
suffixList = List.OfRepeatedItem(
  fillItem,
  Math.Max(0, listLen - List.Count(prefixList))
  );
List.Join([prefixList, suffixList]);

Yes, alternative methods are easy, but I wondered it is a bug of Dynamo to evaluate both sides, or that something else is wrong with my code.

The first alternative is some Imperative block:

But found it a bit overkill so I reduced it to this:

Looks a bit like the solution Mike came with.

But the question remains: why did it happen :smiley: