I believe I have identified a gap in the current features of tiddlywiki in relation to functions.
Background
Functions can be defined using the \function
pragma, and accessed as;
- A variable
<<functioname>>
- A function in filters
[function[functioname]
andfunctionname[]
- other places such as within the backtick attributes filter
$(functionname)$ - ${ [function[]] }$
Functions can also have parameters and default values, but let us put them aside for a moment.
There is however no way to redefine them inline!
Variables
Functions are considered a form of variable and can be addressed as such. In such cases only the first value is returned, but even this can be changed using format:titlelist and join.
Variables can be defined with pragma and made global.
Regular variables can also be redefined in line with $set, $vars, $let and as the result of the $list widget (and possibly others).
Redefining functions inline
Lets say I have a globally defined function I will to use in a large number of filter, for example a default sort order.
\function .sort(default:"title") [sort<title>]
Thus standard filters could be written to include +[.sort[]]
- This allows me to change the default globally
However
If I am using a filter making use of the above function there is no way to redefine it inline, although I could use pragma in a given tiddler, this will override the use of .sort[]
throughout the current tiddler.
A non working example may be;
<$let .sort="[!modified[]]">
<$list filter="[.all[]tag[TableOfContents].filter[]] +[.sort[]]">
</$list>
</$let>
- I understand this does not work and possibly why
- Nor does it permit the parameters settings possible in the
\function pragma
Conclusion
Perhaps we could have a widget $function
or enhance the set widget so we can redefine a function inline, in addition to the current \pragma
so designers can use a global function but locally, within that $function
widget, redefine the function.
Eg Non working example
\function .sort(default:"title") [sort<title>]
<$function name=".sort" default="modified" definition="[sort<title>]">
<$list filter="[.all[]tag[TableOfContents].filter[]] +[.sort[]]">
</$list>
</$function>
- A way to redefine multiple functions inline would be helpful.
Bigger picture
Perhaps a similar or related solution to also allow the inline redefinition of macros, and procedures could be very helpful.
After note:
There are other ways of making use of variables in functions that provide alternatives to this proposed approach, however I see value in the above as useful enhancement that would be inline with the way we use variables. I am not looking so much for a workaround, but a solution as proposed.