Polymorphic macros with/without parameters

I’m trying to implement ‘polymorphic’ macros, that is macros, that have the same name but behave differently depending on the tiddler they are defined in.

Suppose, you have two tiddlers, Tiddler1 containing book data and Tiddler2 containing a picture. Both the tiddlers have a macro called show to show specific details of their content. Of course, the details shown for a book are different from those shown for a picture.

Now, you want to abstract away these details and just tell the tiddlers to show their content. This can be done by using an additional level of indirection:

  • define a field called show
  • put a macro in this field that invokes the local show macro: <<show>>
  • transclude the show field of the tiddlers: {{Tiddler1!!show}}, {{Tiddler1!!show}}

One use case for this technique is for instance a tab widget showing different content for each of its tabs.

Now two questions:

  1. Using the indirection field, how could I pass parameters to the macros.

  2. Is it possible to avoid this level of indirection and call a macro of a specific tiddler directly, i.e. using the common syntax of OOP languages
    MyTiddler.myMacro?

I would consider having the macros respond to variables rather than using parameters.

Importing macros might be relevant: https://tiddlywiki.com/#ImportVariablesWidget

I would also consider transcluding the tiddler via a different template depending on the type of tiddler, rather than using macros.

When you define a macro or variable you are realy redefining it in the current scope eg tiddler, list or set let or vars widget. In leaving that scope the previous value applies.

This can be commonly seen in the different re-uses of the current tiddler variable.

Personaly I write most of my macros to operate with the current tiddler and thus they can be used in any tiddler or list etc. I choose to which tiddler is current when i call the macro.

When writting such macros they have the greatest versatility if they determin in which context they are if they are to behave differently if not act on the current tiddler.

I use variables titles field existence or values to trigger which polymorphisium to use.

1 Like

I think, that’s the way to go. It would also be much easier to understand for others.

1 Like

Like Saq, I believe you need to use templates! There is some efforts using TW in OO paradigm.
Search the old forum there was some discussion on OOP there, but I have not the link

Thank you all for your comments and suggestions!

@saqimtiaz and @TW_Tones

I would consider having the macros respond to variables rather than using parameters.

I didn’t think of variables. This solves my problem. And I can do without this level of indirection.

I would also consider transcluding the tiddler via a different template depending on the type of tiddler, rather than using macros.

I want to avoid to create several tiddlers for one type of data (say a Book or a Person). Similar to a class in OOP languages, I would rather create one type definition tiddler that defines a Book or Person type and contains all the macros pertaining to its special type.

Each type has the same set of macros with the same names (i.e. create, list-all, list-selected) but with type-specific behaviors (thus ‘polymorphic’).

The ‘instances’ of each type then transclude the tiddler with type definition.

@Mohammad

There is some efforts using TW in OO paradigm.
Search the old forum there was some discussion on OOP there, but I have not the link.

Great tip here and here Thanks! So much to absorb…

2 Likes

By the way, @rengel, while writing a reply (or a new message), the area above the reply is fully interactable. You can, for example, highlight text anywhere on the forum and it will pop up a little Quote button for you that you can click. It will plop in a quote directly in the message where you were typing.

Here’s a screenshot snip:

1 Like

Great tip! Thanks for showing an example!

1 Like