From lliao@ren.eecis.udel.edu Tue Nov 19 11:12:06 2002 Date: Mon, 18 Nov 2002 22:33:49 -0500 (EST) From: Li Liao Subject: Re: return types for Method call > After looking at some examples in lecture 8 and 9, I noticed > that function body in those cases was evaluated with the same > eval function. In our case we have 2 cases: eval and exec and > they return different return types. The only way to evaluate > a body of a function is to use exec, but it returns type unit > and we need type Value. > may be if I give you a piece of my code it will be clear what I mean: > > eval (MethodCall (e,m,es)) env ctab = > let ... - evaluate e to an object v, - get info of method m (including its return typy, formal parameters, and mbody) from the ctab of v, - evaluate arguments es - bind arguments to formal parameters (so env is updated and lets call it env') - update "_this" - now we are ready to realy do our job, that is, > in > exec mbody env' ctab > end > (this is the case that confuses me, using exec is the only way to > evaluate a body of a function, but this will return unit and we need > type Value) > Another option will be to return an object like so: > in VObj(oid, oclass, env') end > but then case VNum and VNull are left out. How do we return that case? You raised up a very good question. And I can see that you are almost there. Remember the last command (or statement) of a method body is "Return ...". If you look at how "exec (Return e) ..." raise an exception that takes what e is evaluated to, you may want to handle that "ReturnSignal" exception in "eval (MethodCall ..." to get the returned value. We had used the same trick to give returned values out of a loop in homework 2 (see message 17). For methods that do not have "Return ..." in their body, the defaultValue should be returned depending on the return type of these methods. To put these two cases together, you may want to use another trick that is the magic ";", which was dicussed in message 15. I hope this is helpful. Li