Testing if a macro has none, one or more parameters

69 views
Skip to first unread message

TonyM

unread,
Jan 4, 2019, 12:03:31 AM1/4/19
to TiddlyWiki
Folks,

In the depth of a project I am working on, in part related to Systems Design Strategy - Centralised logic 

I would like to test inside a macro definition if any parameter have being passed, psudo code below

\define mymacro()
If no parameters passed
 
Return what is here
end-if
If parameters passed
 
Return what is here
end-if
\end


The reason is I want to use macros as a filter definition as in the subfilter<mymacro> but when one or more parameters are supplied return a different result.

This will allow the same macro definition to perform multiple functions since parameters can not be passed when used as filters, but when one or more parameters are passed it can use them.

I would like perhaps to be able to test if "params" is empty, where params is a string of all parameters passed to the macro from a macro call.

If this facility is not available I will request it.

Thanks in advance
Tony

Mohammad

unread,
Jan 4, 2019, 1:46:24 AM1/4/19
to TiddlyWiki
Hi Tony,
Not the answer to your question, but just a side note, Mark S has a small code to let you pass and determine the number of parameters passed.
This is useful if you want to have a variable number of parameters.



--Mohammad

TonyM

unread,
Jan 4, 2019, 1:50:13 AM1/4/19
to TiddlyWiki
I just realised the anseer,

Name at least one parameter and test the first fow notempty.

Will publish whenn compete.

Good idea, when count of patameters is zero no parameter passed.

Tony

Mohammad

unread,
Jan 4, 2019, 1:50:26 AM1/4/19
to TiddlyWiki
Further input:

\define test(mylist)
<$list filter="[enlist[$mylist$]]" emptyMessage="Empty">
<$view field="title"/><br/>
</$list>
\end

<<test "a b">>
<<test>>

Results:

a
b


Empty


TonyM

unread,
Jan 4, 2019, 4:46:23 AM1/4/19
to TiddlyWiki
Mohammad,

Thanks for that. It is not dissimilar to the approach I found, however it would be good if we could reference all parameters passed, to the macro, if for no other reason to allow them all to be passed on to a subsequent macro without having to name them.

Regards
Tony

PMario

unread,
Jan 4, 2019, 8:08:51 AM1/4/19
to TiddlyWiki
Hi,

Be aware, that macro parameters are named! So this has to work:

\define test(a, b) $a$ ... $b$

<<test b:"asdf" a:"aaaaa">>

Calling macros with 1 or 2 parameters without names is lazy but ok. If they have more params it's extremely error prone.

have fun!
mario

PMario

unread,
Jan 4, 2019, 8:14:35 AM1/4/19
to TiddlyWiki
Hi,

If you need default values for parameters, you can define them. IMO that's the same as checking for empty values, which isn't possible atm.

\define test(a:"aaa", b:"bbb")
if a and b have default values -> do this
test a -> do this
test b -> do this
\end

have fun!
mario

TonyM

unread,
Jan 4, 2019, 8:34:21 AM1/4/19
to TiddlyWiki
Mario,

Are you suggesting setting the default value for the first parameter to say "default" and when it is default do x and when it is not do y?

In the case of it being used as a subfilter it will always return the default? I will test it.

Thanks for the suggestion.

Tony

PMario

unread,
Jan 4, 2019, 8:44:19 AM1/4/19
to TiddlyWiki
Hi,
For filters it may be possible to make them empty like: " " ... 1 space. A filter will return nothing, but it should be possible to detect the space if needed.
-m
Reply all
Reply to author
Forward
0 new messages