Search code examples
functionargumentsjqsemantics

meaning of $value in function formal parameters


I'm looking at the JQ "builtin.jq" file, and find

def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));

and am trying to figure out the semantics of "$value" as a formal parameter. It could mean that the parameter is expected to provide only one value, not a list of them. Or it could be the same as

def _assign(paths; vv): vv as $value | reduce path(paths) as $p (.; setpath($p; $value));

or maybe it's something else?

I can't find anything in the documentation about this kind of function formal-parameter.


Solution

  • You're right about def _assign(paths; vv): vv as $value ...

    In essence, a formal parameter, $x, is equivalent to having x in the formal parameter list, followed by x as $x shortly thereafter.

    This is briefly mentioned in the jq manual:

    Or use the short-hand:
    
    def addvalue($f): ...;
    

    What is not mentioned is that, using this example, f can also be used in the body of addvalue, though doing so might easily be the source of confusion. For example, what result would you expect the following to produce?

    echo 1 2 3 | jq -n 'def f($x): x+x; f(input)'