[Grace-core] Assignment return values and null

Kim Bruce kim at cs.pomona.edu
Tue Jun 26 10:48:54 PDT 2012


I'm probably the one who argued loudest for not having assignments return a value, so I think I owe a solution.  Good programming practice says one should never be able to ignore an expected outcome -- and end of file is expected with these loops.  Thus I would have myfile.getLine return an OptionString which would be defined as something like
	type OptionString = String | singleton(endOfFile)
where singleton(endOfFile) is a type with only element, the object endOfFile, which would have been defined earlier.  (Of course there might also be an error value -- and the syntax of singleton types is still not fixed.)

Then the loop becomes writeable in several ways.  Here are a couple.  The first uses a priming loop, which should be reminiscent of things done in Pascal long ago):

var newLine: OptionString := myFile.getLine
while {newLine != endOfFile} do {
    match(newLine) 
         case {nl:String -> ...}
         case {_ -> ... should never occur ...}
   newLine := myFile.getLine
}

The extra case is annoying, so an even better solution would be the following:

var done:Boolean := false
while {!done} do {
   match(myFile.getLine)
         case {nl:String -> ...}
         case {endOfFile -> done := true}
}

To me, this last version is nice and clean and is exactly the kind of code I'd like to teach novice students to write.

We may want to introduce a simple break command in Grace, but we don't really need it as a boolean guard can always provide the same facilities.
    
Kim



On Jun 26, 2012, at 3:45 AM, James Noble wrote:

> 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
> 
> _______________________________________________
> Grace-core mailing list
> Grace-core at cecs.pdx.edu
> https://mailhost.cecs.pdx.edu/mailman/listinfo/grace-core



More information about the Grace-core mailing list