Making StreamReader Iterable
Posted: Sun Jan 26, 2014 8:44 pm
I tried to make StreamReader iterable with the following:
I get the following errors:
What did I do wrong? Thanks.
extend StreamReader
"""Makes StreamReader iterable (by line)"""
cue enumerate as String*
"""Yields each line in the open file"""
test
# Make and test a file:
File.writeAllText("Source", "line 1\nline 2\nline 3")
lines = []
using file = File.openText("Source")
for line in file, lines.append(line)
assert lines == ["line 1\n", "line 2\n", "line 3"]
File.delete("Source")
# Make and test an empty file:
File.writeAllText("Source", "")
lines = []
using file = File.openText("Source")
for line in file, lines.append(line)
assert lines == []
File.delete("Source")
body
while true
line = .readLine
if line is nil, break
yield line to !
I get the following errors:
- Code: Select all
iterateFile.cobra(23): error: Cannot infer the type for "line" from the "for" loop.
iterateFile.cobra(35): error: Cannot infer the type for "line" from the "for" loop.
What did I do wrong? Thanks.