[Grace-core] Assignment return values and null
Bart Jacobs
bart.jacobs at cs.kuleuven.be
Tue Jun 26 04:33:03 PDT 2012
There's always recursion:
def iter = { ->
def line = myfile.getline
if (!myfile.eof) {
print(line)
iter.apply()
}
}
iter.apply()
Could be made slightly nicer as follows:
recurse { me ->
def line = myfile.getline
if (!myfile.eof) {
print(line)
me.apply()
}
}
where
method recurse(body) { body.apply(body) }
Or still nicer perhaps:
loop { break ->
def line = myfile.getline; if (myfile.eof) { break.apply() }
print(line)
}
where
method loop(body) {
def myBreakException = new Exception
try {
def break = { -> throw myBreakException }
while {true} do {
body.apply(break)
}
} catch { (myBreakException) -> }
}
On 26/06/2012 12:45, 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
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
More information about the Grace-core
mailing list