[TW5] Problem with macros and default values

35 views
Skip to first unread message

FrD

unread,
Sep 28, 2015, 1:03:37 PM9/28/15
to TiddlyWiki
Hi,

Still exploring macros and default values.
I've got a problem in a macro calling a second macro. The first macro has some arguments with default values.

Here's an MTC.
Create a tiddler (say TestMacros) with a field "foo" with value "bar".
Put this code in the tiddler :

\define testmacro1(alpha:{{!!title}} beta gamma:{{!!foo}})
<<testmacro2 $alpha$ $beta$ $gamma$>>
\end

\define testmacro2(alpha beta gamma)
alpha
= $alpha$
<br>
beta
= $beta$
<br>
gamma
= $gamma$
\end

<<testmacro1>>

The result is :

alpha = TestMacros
beta = bar
gamma =

Seems to me that beta should'nt hold any value and gamma should hold "bar" as its value.
Could someone explain this behaviour ?

Thanks

FrD

Jeremy Ruston

unread,
Sep 28, 2015, 1:20:38 PM9/28/15
to TiddlyWiki
Hi FrD

There are a couple of things going on:

* Parameters in a macro definition are separated with commas. It is unfortunate that this is different than the white space separator used when invoking macros
* In your example, the macro invocation of testmacro2 is unpacking to <<textmacro2 {{!!title}} {{!!foo}}>>. The missing value for the beta parameter comes out blank, causing the value you've specified with $gamma$ to be passed as the parameter "beta" to testmacro2. The workaround is to use named parameters and quotes:

Here's a corrected version:

\define testmacro1(alpha:"{{!!title}}",beta,gamma:"{{!!foo}}")
<<testmacro2 alpha:"""$alpha$""" beta:"""$beta$""" gamma:"""$gamma$""">>
\end

\define testmacro2(alpha,beta,gamma)
alpha = $alpha$
<br>
beta = $beta$
<br>
gamma = $gamma$
\end

<<testmacro1>>

Best wishes

Jeremy


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at http://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/a21ed836-2926-4468-bc9b-aae7de4f5a80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jeremy Ruston
mailto:jeremy...@gmail.com

FrD

unread,
Sep 28, 2015, 1:28:52 PM9/28/15
to TiddlyWiki, jeremy...@gmail.com
Hi Jeremy,

Thanks for the quick answer !
I did'nt know these details.
I think the documentation on tiddlywiki.com should be updated : the example in the tiddler "Macro Definitions in WikiText" doesn't use commas to separate the parameters.

FrD

Jeremy Ruston

unread,
Sep 28, 2015, 2:08:14 PM9/28/15
to FrD, TiddlyWiki
Hi FrD

Thanks for the quick answer !
I did'nt know these details.
I think the documentation on tiddlywiki.com should be updated : the example in the tiddler "Macro Definitions in WikiText" doesn't use commas to separate the parameters.

Oops, you're quite right, or rather my past self was correct: macro definitions can use spaces or commas to separate parameters. As the documentation says: "The parameter separator (sep) is any sequence of characters that does not match a param-name. Among other things, this includes commas, spaces and linefeeds":


Best wishes

Jeremy.

 


FrD


Le lundi 28 septembre 2015 19:20:38 UTC+2, Jeremy Ruston a écrit :
Hi FrD

There are a couple of things going on:

* Parameters in a macro definition are separated with commas. It is unfortunate that this is different than the white space separator used when invoking macros
* In your example, the macro invocation of testmacro2 is unpacking to <<textmacro2 {{!!title}} {{!!foo}}>>. The missing value for the beta parameter comes out blank, causing the value you've specified with $gamma$ to be passed as the parameter "beta" to testmacro2. The workaround is to use named parameters and quotes:

Here's a corrected version:

\define testmacro1(alpha:"{{!!title}}",beta,gamma:"{{!!foo}}")
<<testmacro2 alpha:"""$alpha$""" beta:"""$beta$""" gamma:"""$gamma$""">>
\end

\define testmacro2(alpha,beta,gamma)
alpha = $alpha$
<br>
beta = $beta$
<br>
gamma = $gamma$
\end

<<testmacro1>>

Best wishes

Jeremy


Reply all
Reply to author
Forward
0 new messages