Passing arguments to DekiScript functions

There are two ways to pass arguments to a Dekiscript function.  Both have their uses, so it is good to be comfortable with both when diving into Dekiscript.  This page is a tutorial-styled expanson of this reference page.

For this page, we'll use the wiki.tree() function as an example, and will freely intermix the terms "parameter" and "argument".  Wiki.Tree() specifies the following parameters:

Name Type Description
path str (optional in 8.08 or later, default: current page) path to the page
depth num (optional, default: complete depth of tree) depth of children to return (1.8.2 or later)
reverse bool (optional, default: false) list pages in reverse order (1.9.1 or later)

Passing a Parameter Map

The function parameter list is actually a map, which consists of a series of key/value pairs. Each parameter is an entry in the map, with the key equal to the parameter name, and the value equal to the one you provide.  So our first way to pass parameters is to pass the function the parameter map directly.

A map consists of curly braces "{}" enclosing a comma-separated list of name:value pairs.  One nice aspect of this approach is that you need only specify the parameters you want to, in any order, and omit the optional parameters you don't care about.

Example 1

So let's say we want to produce a complete site map, using the default sort order.  We can say this:

{{ wiki.Tree{ path:"" } }}

We've only specified the "path" parameter, and set it to "", which will start from the root of the wiki.  The optional "depth" and "reverse" parameters are left to their defaults.

Example 2

Now let's say we want to reverse order of the listing:

{{ wiki.Tree{ reverse:true, path:""  } }}

Because I'm using a map, I can skip the middle argument and not worry about it.  And just for fun, I've reversed the order of the arguments.  Doesn't matter!

Passing an Arg-list

Dekiscript also gives you a convenient shorthand option to pass the parameters as an arg-list, and then it'll convert it to a map for you. In this case, you enclose the list in regular parentheses "()", and just provide a list of the values you wish to specify in the order listed in the documentation. Let's recreate our above examples.

Example 1

Here's our new version of the first example:

{{ wiki.Tree( "" ) }}

When using an arg-list, I can always omit optional arguments at the end of the list.  So, as before, the optional "depth" and "reverse" parameters are left to their defaults.

Example 2

So how would the second example look?

{{ wiki.Tree( "", nil, true ) }}

This is a bit more interesting.  Unlike with the map, we must specify the arguments in the correct order.  Further, we couldn't just skip the second one, or Dekiscript would have no way of knowing that the last argument was really the "reverse" parameter.  So we need to fill the second argument with nil, which is functionally equivalent to not setting the argument at all, but acts as a place-holder in the list.  Instead of nil we could also have used the synonyms of an underscore (_), or null.

How to choose?

Personal preference, of course.  The map syntax is more robust.  You don't need to remember the argument order, and you don't need a string of nils if you only want to set the very last optional parameter.  That is both hard to read and easy to get wrong.  On the other hand, the arg-list syntax is more compact and convenient for function calls with only a few arguments.

 
 
Tag page
You must login to post a comment.