[Grace-core] Assignment return values and null
James Noble
kjx at ecs.vuw.ac.nz
Tue Jun 26 03:45:26 PDT 2012
Hi Jan
that's a good question!
In the case of looping over a file, I guess I'd hope for something like
> for (myfile.lines) do { line ->
> print (line)
> }
but that doesn't help the larger problem.
I'd have thought the code below could work
> var line
> while { line := myfile.getline; !myfile.eof } do {
> print(line)
> }
James
On 26/06/2012, at 19:19 PM, Jan Larres wrote:
> Hi,
>
> I just came across a situation that doesn't really have clean solution
> in Grace. If you want to read a line from a file and check whether the
> end has been reached you can do this in C:
>
> while ((read = getline(&line, &len, fp)) != -1) {
> printf("%s", line);
> }
>
> Or this in Java:
>
> while ((line = br.readLine()) != null) {
> System.out.println(line);
> }
>
> In Grace you can't do it in such a clean way since assignments don't
> return their assigned values and there are no nulls. I can think of two
> ways to do this currently:
>
> while { !myfile.eof } do {
> var line := myfile.getline
> if (!myfile.eof) then {
> print(line)
> }
> }
>
> var line := myfile.getline
> while { !myfile.eof } do {
> print(line)
> line := myfile.getline
> }
>
> but they're both neither really intuitive nor pretty since they require
> unnecessary duplication, and the second one is kind of backwards.
>
> I've also tried this:
>
> var line
> while { line := myfile.getline; !myfile.eof } do {
> print(line)
> }
>
> but that fails with a "use of undefined identifier line" in the print
> statement (this may be an implementation issue, I haven't checked yet).
>
> Is there a specific reason why assignments don't return their value? I
> can't see any harm in doing that. The null is of course a more
> controversial issue, but at least at the moment there doesn't seem to be
> a way to implement a getline() method in a way that indicates the end of
> the file instead of just an empty line.
>
> Jan
More information about the Grace-core
mailing list