7

Assume we are defining a canvas abstract data type in METAPOST, where some tasks are gathered. I would like to define a kind of reference to the last added task, but I do not know METAPOST good enough to figure out which macro concept I should use.

Here is a no-op code illustrating the use of such a reference:

def task text TASK =
  vardef TASK.init =
  enddef;
enddef;

def canvas text CANVAS =
  numeric CANVAS._task_n;
  task CANVAS._task[];

  vardef CANVAS.init =
    #@._task_n := 0;
  enddef;

  vardef CANVAS.current_task =
    #@._task[#@._task_n]
  enddef;

  vardef CANVAS.add_task =
    #@._task_n := #@._task_n + 1;
    % #@.current_task.init; % ! Yield an isolated expression `init`
    #@._task[#@._task_n].init;
  enddef;

enddef;

canvas c;
c.init;
c.add_task;
bye;

How can I correctly define the current_task macro, so that I can modify the add_task macro to refer to the current_task instead of #@._task[#@._task_n] (as suggested in the comment)?

  • I'm an regular user of MetaPost, but can't understand the question. Maybe you should ask the MetaPost mailing list: tug.org/mailman/listinfo/metapost The MetaPost developers and top-specialists will probably be able to answer. – Franck Pastor Mar 27 '14 at 7:34
  • I would like to refer to #@._task[#@._task_n] in a symbolic way, i.e. to be able to write something like #@.current_task.init instead of #@._task[#@._task_n].init but my current approach does not work. Thank you to point out the mailing list. – Michael Le Barbier Grünewald Mar 27 '14 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.