Skip to content

Using function for assignation before inner for-loop #378

Description

@PieroM97

Hi there,

I’m experimenting with JSLT and ran into something I don’t fully understand. I’m trying to assign the result of a function to a variable using let, and then use that variable inside a nested for expression.

Here’s a simplified version of my code ( to test on demo playground ) :

def extract_and_add_text(values)
    [
        for(flatten([$values])) 
            let current_value = hello(.)
            [for([$current_value])
                ( .)]
    ]

def hello(text)
    "Hello " + $text + "!"

{
    "url_update" : extract_and_add_text(.url),
    * : .
}

In this case, i have a syntax exception: it seems like i can't use the for loop. But if I assign the value directly without passing from a function, the nested for works fine. Like this:

def extract_and_add_text(values)
    [
        for(flatten([$values])) 
            let current_value = (.)
            [for([$current_value])
                ( .)]
    ]

def hello(text)
    "Hello " + $text + "!"

{
    "url_update" : extract_and_add_text(.url),
    * : .
}

I have found different workarounds to this """""problem""""", but i would just like to understand why we arent allowed to use a function to for assignation before inner for-loop.

Just for information, one possible workaround is to use the function inside the inner loop, for example:

def extract_and_add_text(values)
    [
        for(flatten([$values])) 
            let current_value = (.)
            [for([$current_value])
                hello( .)]
    ]

def hello(text)
    "Hello " + $text + "!"

{
    "url_update" : extract_and_add_text(.url),
    * : .
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions