Skip to content
Jing Lu edited this page Jun 19, 2013 · 13 revisions

ReoScript support to enumerate over an object which implementing the IEnumerable interface in .NET.

Iterator Syntax

To iterate over an object using the for ... in syntax.

The array in ReoScript can be enumerated over every elements. (doesn't like JavaScript)

var arr = ['a', 'b', 10, 30];

// iterate array
for (element in arr) {
  console.log(element);
}

The result is:

a
b
10
30

Enumerate over an object to get all names of property.

var obj = { a: 10, b: 20 };

for (key in obj) {
  console.log(key + " : " + obj[key]);
}

The result is:

a : 10
b : 20

String can also be enumerated over every characters.

Enumerator Support and Extension

Any .NET type implements IEnumerable interface will be available to enumerate in script. ReoScript support to enumerate the following interface:

  • IEnumerable
  • IDictionary<string, object>

For example:

public class StringObject : ObjectValue, IEnumerable
{
  public string String { get; set; }

  public override IEnumerator GetEnumerator()
  {
    for (int i = 0; i < String.Length; i++)
    {
      yield return String[i];
    }
  }
}

Now StringObject supported to iterate using for...in syntax:

var a = 'hello';

for(c in a) {
  console.log(c);
}

The result is:

h
e
l
l
o

Dynamic Type & ExpandoObject

ExpandoObject class is available in .NET Dynamic Language Runtime, which provides an ability to add or remove members of object at .NET Run-time. ExpandoObject implements the IDictionary<string, object> interface so can also be supported to enumerate in ReoScript.