Question: Choosing between table vs. Record for passing information between functions?

I am trying to decide which is better to use, a table or Record, to pass lots of information back and forth between functions inside say a personal Maple package.

So instead of passing each argument on its own, as normally one does when calling a function, instead all the input is put into one table, or into one record, and this is passed instead. So the function always takes as input one argument.

When the function returns result, it also does the same. It updates any fields it needs in the table or in the record, o rmay be add new field, which the caller would have to know about, (since same person has programmed both) and returns that.

I find this easier to handle, than passing each argument on its own.  And on return, rather than return many results in list or sequence and making sure they are in correct order, the function simply update the fields in the table or the record. No problems with wrong order here as caller will read these fields by name later on.

So I made same toy example, one using table and one using record. 2 numbers are passed to a function. The function returns the result of multiplication by adding new field into the table/record.  Here is version using table.

restart;
foo:=proc(input::table)
   local x,y,result,updated;

   x      := input["x"];
   y      := input["y"];
   result := x*y;

   #finished computation. Return result. Copy all content
   #of passed table first, then add new field to write result to

   updated := eval(input);
   updated["result"] := result;
   return updated;      

end proc:

Called using

input:=table(["x"=4,"y"=6]);
r:=eval(foo(input)):
print(r["result"]);

prints 24

 

Here is version using Record

foo2:=proc(input::record)
   local x,y,result,updated;

   x      := input:-x;
   y      := input:-y;
   result := x*y;

   #finished computation. Return result
   #could not update input Record in place, even after deep copy
   #since "result" is not an existing field. So have to make a new Record
   #this is a problem, since this function need to know all fields now in Record

   updated := Record("x"=x,"y"=y,"result"=result);
   return updated;      

end proc:

called as

input:=Record( "x" = 4, "y"=6);
r:=eval(foo2(input)):
print(r:-result);

prints 24

 

Some observations: I found the table more flexible. Because the function called can add a new field to the table, even if this field do not exist. If called function wants to add a new result back to the caller, it can do this.  With Record, all fields have to be decided on when the record is created. So the function has to create new record from all the old fields and add the new field to do this.

This might not be a big problem actually, as one can decide at top of package what all the possible fields that needs to be in the Record, and creates such Record, giving default values for al field. So now Record will have all possible fields in it.  Using this the above can now be written as

restart;
foo3:=proc(input::record)
   local x,y,result;

   x      := input:-x;
   y      := input:-y;
   result := x*y;

   #finished computation. Return result  
   input:-result := result; 
   return input;      

end proc:

And called as

input:=Record( 'x' = 4, 'y'=6, 'result'=NULL);
r:=eval(foo3(input)):
print(r:-result);

24

But it seems both methods suffer from the fact that type checking on the types of the arguments now inside is lost. (But I seem to remember one can do type checking on fields for a Record somehow). So if this is possible, this will be one advantage of Record over table.  But again, it is possible in both methods to check type of field by using whattype() or type() manually if needed.

But other than these two issues, and assuming one wants to pick between table and record, the question is, why choose Record over table?  If the main purpose is to just to pass information back and forth? Is there some hidden advatage to using Record I am overlooking that tables do not provide (again, for the purpose of passing arguments and results between functions and no more).

For me, I see table doing what I want from a record, but a little bit easier to work with as well (field names can be strings for example).  So with table one does  A["x"]=.....  While with Record one does A:-x=...

For me, I see a Maple table just like Python dictionary, so easy to underatand and work with.

Any thoughts from the experts on this subject? 

Please Wait...