elan-language / LanguageAndIDE

Apache License 2.0
3 stars 1 forks source link

readToEnd doesn't strictly do what the name suggests #842

Closed stirlingstout closed 2 weeks ago

stirlingstout commented 3 weeks ago

Looking at the source for text-reader it looked as though the readToEnd function doesn't, but does more a readAll, and testing confirms this.

So

var f set to openFileForReading() var line set to f.readLine() print line set line to f.readLine() print line let rest to f.readToEnd() print rest call f.close()

prints the first line of the file, the second line of the file, then all of the file including the first and second lines.

I think it's unlikely you'd ever want to do this, but it is possible, and I think the mismatch between name and implementation is the major problem.

Suggestions:

Incidentally the test above demonstrates the issue about line terminators mentioned in #833 since it prints the first two lines with both a terminator from the string being printed and the terminator from print:

118

135

118
135
197
219
245
264
314
321
356
447
471
486
511
544
566
593
625
638
661
751
784
799
842
867
891
912
975
996
richardpawson commented 2 weeks ago

Fixed

richardpawson commented 2 weeks ago

On further reflection, I think that having two methods makes things more confusing because, if you are at the start of the file readToEnd and readAll are the same (so lots of 'Which one should I use, Sir?' followed by 'Why?'). Also the need for the (revised) readToEnd is pretty rare - and you can easily do it as a loop yourself in Elan if needed.

So I will reduce the read... methods back to just:

readLine() readWholeFile()

richardpawson commented 2 weeks ago

Also, for symmetry, I am adding writeWholeFile - in both cases this automatically closes the file.