Jermolene / TiddlyWiki5

A self-contained JavaScript wiki for the browser, Node.js, AWS Lambda etc.
https://tiddlywiki.com/
Other
7.78k stars 1.16k forks source link

True Variables Missing? #4013

Open AnthonyMuscio opened 5 years ago

AnthonyMuscio commented 5 years ago

I stand to be corrected and would be happy to be so. However I believe with the introduction of the maths operators we have something missing from tiddlywiki. I will just say it how I see it and respond to any requests for clarification.

A True Variable, that can referenced, independent of context, or at least in a specific context. For want of a better name "non-local variables".

Whilst macros can be made global they are evaluated at run time. This is really nice because you can use recursion like in the TOC's and each iteration the variable name is "reused". However if you want to for example increment a variable you may increment the current variable but this is lost when you return to the prior level. You can pass a value up, but any changes to it are not returned to the calling process.

The only way I see to accrue or increment a number across processes is to use a field or text reference and to change these you must use an action that needs a trigger.

For example if I had three lists, themselves with nested lists and in some of the nested lists I would like to increment a number to score with special conditions, in more than one of the nested lists, I cant do it without wrapping the whole thing in a button and using an action widget to save the value after every increment. This forces a user trigger/interaction and the creation of separate independent set of nested lists one to iterate it for the user, one to power a button to compute the value.

If I come with a problem I should suggest a possible solution. What about a widget that operates like this? Perhaps for scoring?

<$set-nonlocal name=myvar value="0">
...
<$list ...
   <<myvar "{{{ [<myVar>add[2]] }}}">>
</$list>
<$list ...
   <$list ...
      <<myvar "{{{ [<myVar>add[1]] }}}">>
   </$list>
   <<myvar "{{{ [<myVar>add[3]] }}}">>
</$list>

Total score <<myvar>>
</$set-nonlocal>

Where every reference to<<myvar>> or <myvar> is to the non-local variable?

Or perhaps a complementary set macro that only operates on non-local variables. <<set-nonlocal myvar "{{{ [<myVar>add[1]] }}}">>

Regards Tony

twMat commented 5 years ago

@AnthonyMuscio I think your post can be summed up in one sentence; "I want global variables" :-)

pmario commented 5 years ago

In TW every tiddler is a global variable.

twMat commented 5 years ago

In TW every tiddler is a global variable.

OK, I guess that was a reply to me; Sure, point taken, so I rephrase:

@AnthonyMuscio I think your post can be summed up in one sentence; "I want global variables that are not stored" :-)

AnthonyMuscio commented 5 years ago

If every tiddler is a variable why do I need an action widget or user interaction to set or change the the values there in?

why are all variables transient in a widget tree and I cant even have a trigger on open tiddler which could allow me to emulate a nonlocal variable in some cases.

Tony

pmario commented 5 years ago

This doesn't need a user interaction

\define global()
<$set name=myVar value={{{[<myVar>add[2]]}}}>
<$set name=myVar value={{{[<myVar>add[1]]}}}>
<$set name=myVar value={{{[<myVar>add[3]]}}}>
<<myVar>>
</$set></$set></$set>
\end

<<global>>

I'm not 100% sure, what you want to achieve.

pmario commented 5 years ago

macros are internally handled like variables. ... But they can be global, if tagged $:/tags/Macro

pmario commented 5 years ago

If you use global variables for calculation, your users may end up with problems, since they don't care about duplicated names. ... math algorithms shoudn't have side effects.

AnthonyMuscio commented 5 years ago

Mario,

Take this as an example, though it does not fully demonstrate what I am looking for.

I have to set a field or tiddler, and recover that if I want to use it and write it back.

\define myVar() 2
myVar= <<myVar>>

<$set name=yourVar value="5">

<$set name=myVar value={{{ [<myVar>] [<yourVar>] +[sum[]] }}}>

myVar= <<myVar>> new local value

<$button set="$:/config/myVar!!text" setTo=<<myVar>> >
Save ~MyVar as a global value
</$button>

Stored value {{$:/config/myVar!!text}}

</$set></$set>

myVar= <<myVar>> Not changed

<h2>The in another tiddler I can use</h2>
Stored value {{$:/config/myVar!!text}}

Regards Tony

pmario commented 5 years ago

@AnthonyMuscio ... IMO github broke your code formatting

AnthonyMuscio commented 5 years ago

Lets back of global a little and as what if I had a set of lists and calculations in one tiddler. If I want the users to select multiple numbers then add them together I cant reference a single variable that acrues the users responces without readin and writing to text references.

What If i had a dashboard with dozens of numbers I wanted to sum. I have to store it in a text reference.

The need is to be able create a variable that I can action in multiple contexts. If I define a variable with set cant set it again in another set statement as they must be closed in each macro or transclusion.

Is this understandable?

twMat commented 5 years ago

Is this understandable?

Frankly, your English teacher would cry ;-) + I agree with pmario that your previous post has broken formatting (that, left uncorrected, remains a problem for everyone reading it)

If I want the users to select multiple numbers then add them together I cant reference a single variable that acrues

The act of selecting is an action so you couldn't you trigger some event here.

From my limited experience in actual coding I remember there were strong arguments against global variables e.g.

Jermolene commented 4 years ago

Hi @AnthonyMuscio @twMat @pmario I think you may be getting at something I've been calling "lateral variables". It's really a TWX thing, and not something that can be changed without impacting backwards compatibility.

The current situation is that variable values are inherited by child nodes in the widget tree from their ancestors. There is no way for a widget to influence the value of a variable for a sibling. That means that it isn't possible to, for example, increment a counter each time a macro is called within a tiddler.

"Lateral variables" refers to the idea that variables be given the ability to inherit values between siblings. Another way of looking at it is that variable values would become part of the evaluation context instead of the widget tree.

The trouble is that lateral variables sets up more dependencies across the widget tree, which may make the refresh process less efficient.

AnthonyMuscio commented 4 years ago

Jeremy

It sounds like you are finding the essence of my need despit my strugling to find the lingo. I will come back with more clarity and some use cases.

Mat please forgive my text, I was struggling to find the words and thought of this way late at night. I should have left it for the next day. Also I do not know the markup for code on my mobile. I have being using the buttons on my desktop.

AnthonyMuscio commented 4 years ago

Ok, If I may try and express myself once again. I appreciate your patience

I think the answer is another kind of variable that can be defined similarly to the set widget however in effect it refers to a memory location, and we may have a mechanism that allows us to manipulate that same memory location any where in the widget tree. Here is a semi-psudo code

<$set-var name=mynumber value=0>
;mynumber is only available between this open and close set
:however we can do this with a matching macro
<<set-var mynumber "[[mynumber]add[1]]">>
:The above will make mynumber = mynumber + 1
<$list filter="firstfilter3items">
  <<set-var mynumber "[[mynumber]add[2]]">>
<$list>
:now mynumber=7
<$list filter="secondfilter2 items">
  <<set-var mynumber "[[mynumber]add[2]]">>
<$list>
:now mynumber=11
<<set-var mynumber "[[mynumber]] =[[othernumber] +[product[]]">>
:now mynumber=11 x othernumber
</$set>

Now imagine a way to use set-var for the whole wiki, or just on a tiddler but the macro allows us to modify it. Some uses will be irrational, others very helpful, but at least we can do it.

Of course we may often need to reset the value.

This makes me think I can do a set of macros to achieve the same.

Regards Tony

AnthonyMuscio commented 4 years ago

This makes me think I can do a set of macros to achieve the same.

Only I will need to have buttons to trigger saving to a field or tiddler.