""" ImplementIEnumerable1.cobra Implementing the IEnumerable interface allows an object to be used in a `for` loop such as: for record in fileCabinet print record and to be passed wherever an IEnumerable is expected such as the List initializer: records = List(fileCabinet) See also: ImplementIEnumerable1.cobra """ class FileCabinet implements IEnumerable var _records = List() def newRecord(name as String) as Record r = Record(_records.count+1, name) _records.add(r) return r def getEnumerator as IEnumerator return _records.getEnumerator def getEnumerator as System.Collections.IEnumerator implements System.Collections.IEnumerable return .getEnumerator class Record cue init(serialNum as int, name as String) base.init _serialNum, _name = serialNum, name pro serialNum from var as int pro name from var as String def toString as String is override return '[.typeOf.name]([_serialNum], [_name])' class Program def main # build a cabinet: cabinet = FileCabinet() cabinet.newRecord('Red') cabinet.newRecord('Blue') cabinet.newRecord('Green') # because cabinet is an IEnumerable, it can be for'ed: for record in cabinet print record # and passed to a List initializer: records = List(cabinet) for record in records assert record.serialNum > 0