Hacker News new | ask | show | jobs
by meta 4978 days ago
Cool idea.

I am confused by the implication of this code

  var prices = new(Table, String, Int);
  put(prices, $(String, "Apple"),  $(Int, 12)); 
Stack objects are created with "$" so these are on the stack and then placed into the Table? So if this was in a function and the function returned, the Table (lets assume it was global) would now be pointing to destroyed stack variables? Is that the correct interpretation?

Is this all done with header files and the preprocessor? It looks like that is the case - if so, I am impressed at the dances you got it to do ;) Also, have you read http://www.amazon.com/Interfaces-Implementations-Techniques-... which does some "Object Orientation" in C tricks?

1 comments

I'm just guessing here, but I assume the variables are created on the stack, then copied into the table object, rather than references being passed to the table. So when the function would end, the original variables would be destroyed, but the copies would remain.
This is correct. "Table" is a datatype which copies the values into the table, while "Dict" is a data type that holds references to the objects. The same semantics hold for "Array" and "List".

Although not perfect this was probably the best way I could have designed such structures. There is some info in the header files as to how to use them.