[Grace-core] Fwd: minigrace oddness

Kim Bruce kim at cs.pomona.edu
Wed Jun 25 16:03:22 PDT 2014


[I sent an earlier version of this with the wrong return address that is being held for release.  It can be deleted.]

I made a stupid mistake in programming Grace that had me confused for quite a while.  While the mistake was stupid, my attempt to fix it led me to something surprising that I'd like to illustrate.

Consider the following Grace program:

def o = object {
   method ford(cond)do(block) {
       print "in for"
   }

   method m(range) {
       for (range) do {n ->
           print(n)
       }
   }
}

o.m(1..3)

When you run it, it prints 1,2,3 (each on a separate line as expected.  The stupid mistake I made (in a much more complex program) was actually writing it as 

def o = object {
   method for(cond)do(block) {
       print "in for"
   }

   method m(range) {
       for (range) do {n ->
           print(n)
       }
   }
}

o.m(1..3)
-------------

By changing "ford" to "for", I accidentally overrode the "built-in" method for()do() and so it printed "in for" instead.  In retrospect this was an obviously stupid mistake on my part.  However, to try to recover I thought I could just annotate the for method in m to be outer.

def o = object {
   method for(cond)do(block) {
       print "in for"
   }

   method m(range) {
       outer.for (range) do {n ->
           print(n)
       }
   }
}

o.m(1..3)
-------------------

However this gave me the following error message:

Error around line 7 of new9: RuntimeError: no method for()do in new9.
 called from new9.for()do (defined nowhere in new9 module) at new9:7
 called from Object.m at line 13 of new9
 called from execution environment
  6:     method m(range) {
  7:         outer.for (range) do {n ->
  8:             print(n)

Is this the right thing that should happen?  While "for" is a built-in control structure, I would have assumed that it would be treated as a top-level method, at least enough so to get access with an "outer".  Of course this may just be an accidental issue with the current minigrace implementation.  What do we want the answer to be?  My inclination is that annotating it as "outer" should have worked, but I'd be happy to hear arguments to the contrary.

Kim

P.S.  My workaround was to define the following function at the top level and use that in method m.

method outerfor(lst)do(block) -> Done is confidential {
  for(lst)do(block)
}




More information about the Grace-core mailing list