Skip to content
Jing Lu edited this page Jun 17, 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 available in Dynamic Language Runtime provides the ability to add or remove property members at .NET Runtime, which implements the IDictionary<string, object> interface so can also be enumerated in ReoScript.

For example: