[Grace-core] Assignment return values and null

Andrew P. Black black at cs.pdx.edu
Tue Jun 26 10:59:18 PDT 2012


This code should not be very complicated.  Please don't try to translate C or Java into Grace, if there are more Graceful solutions.

The decision not to have assignments return a value was deliberate: some of us felt that assignments used as expressions make code really hard to read.  

For the particular case of trying to read a line unless the file is exhausted, I would assume that the file-reading library would have a method 

	method nextLineIfExhausted { action: Block } -> String
	// reads the next line from this file.   A line is a string terminated by the
	// this file line separator sequence (which is not included in the result). or by
	// the end of the file.   If the end of the file has already been reached, this method
	// will execute action and then return the empty string.

Using this, I would write

	method readAndPrintLinesFrom(f: FIle) {
		repeat { nextLineIfExhausted { return } }
	}

assuming that the basic control structures library has a repeat {action: Block} method.   That's a method
that the compiler might treat specially, but it could of course be defined as follows

	method repeat { action: Block } {
		action.apply
		self.repeat { action }
	}
		
in which case we better have tail-recursion elimination working!

Yes, we are aware that the above method readAndPrintLinesFrom can't be inlined, because while return always means "return from this method",  there is no way to say jump out of this loop to an arbitrary named point.   Smalltalk has had the same restriction for 30+ years, and the result is rather readable code.  At one point I did propose a generalization of the syntax so that one could name blocks and exit from any named block, but this proposal did not find favor.  (The discussion is on the Wiki on a page called "A modest proposal" https://projects.cecs.pdx.edu:8443/~black/NewOOL/index.cgi/wiki/NamingEverythingDiscussion
)


 


More information about the Grace-core mailing list