TLDR;
There has been some discussion at GitHub, to allow users to create procedures with the <$set
widget, but it has not been implemented yet.
But even it it will be implemented in the futere, I doubt it will work in a way you expect it. We will need to keep the one parse run and the parent - child structure. – IMO mainly for speed reasons.
For some high-level languages it is OK to define variables near, where they are used, because it makes maintenance easier. But also there it depends on the usecase of the variables. – And most of those languages are compiled. So it is possible to do several parsing runs and compiling speed does not matter at runtime.
That’s different with wikitext. For speed reasons we can only parse the wikitext “code” in a linear run. So it is not possible to define a “tiddler global” variable at the end of the content and use it globally in the tiddler.
\function, \procedure, \define, \widget
are pragmas and have to be at the top of the content for exactly that reason. – Parsing speed
I you have a closer look at the following code
\procedure outer() outer text
\procedure inner() inner text
\procedure innerMost() abcd
It will create a widget-tree as seen in the screenshot:
- All 3 procedures are converted to set-widgets
-
outer is the first one and it has a children array
-
inner is a child of outer - and also has a children array
-
innerMost it a child of inner … and so on.
This makes sure, that body text can call them as macros. Because body text will be children of innerMost. That way, children do automatically inherit variables from their parents.
So looking up “global” variables is as simple as going from parent to parent and use the first variable name that fits.
The TW widget tree is very similar to the HTML DOM tree, which is no coincidence, since the goal is to create HTML as an output.
Conclusion: see TLDR;
Hope that makes sense
-mario