Correct way to use a function defined in another tiddler

I took the example function from https://tiddlywiki.com/#Functions and saved it in a tiddler named function multiply-by-two.

I tried to use the function in another tiddler like this:

{{[function multiply-by-two]}}

<<multiply-by-two 4>>

No luck.

I went to read the docs then tried

\import [[function multiply-by-two]]

<<multiply-by-two 4>>

This worked.

Is this the correct way? Is it common practice to have one tiddler per function like this (I think Grok TiddlyWiki suggests having many small tiddlers containing small atomic pieces of data, but I’ve also seen multiple functions/macros declared in the same tiddler)? Is the import mechanism transitive (as in if I have a tiddler function foo, then a tiddler module bar which imports function foo, do I get access to function foo if I import module bar)?

While I understand (I think) the advantages of importing over sourcing, why didn’t the first version work?

When you import a tiddler, you import all the functions, macros, procedures defined in it. So you could also have multiply-by-three in the same tiddler, and have them both available where you imported them.

No, importing is not transitive.

I don’t know why it’s designed that way. But it’s not something I’ve ever thought to try.

I rarely use imports and have never tried your other method. I usually use $:/tags/Global (or in earlier versions, $:/tags/Macro) on those tiddlers whose functions I want to reuse elsewhere. It might make sense to avoid doing that in a plugin, where we could run into namespace collisions, but in my own code it seems fine.

Note that content you use in the transcluded tiddler does work properly, calling the internal functions. Those functions are simply not available in the outer transcluding environment.

2 Likes

Thanks for the tip on using $/tags/Global to stay current - Still using $:/tags/Macro - missed that change.

1 Like