Skip to content

Forward-referencing functions may result in unexpected behaviour #1919

Open
@bertbaron

Description

@bertbaron

Bug Report

1. Minimal reproduce step (Required)

1 Create a file example.k:

a = wrappedIdentity(1)
b = identity(2)

wrappedIdentity = lambda item {
    identity(item)
}

identity = lambda item {
    item
}

2 run with kcl example.k

2. What did you expect to see? (Required)

a: 1
b: 2

If KCL is not supposed to call functions before they are declared I would expect KCL to fail already on the first line of course but since it seems to work as expected in almost any case I assume it is supported (but couldn't find any statement about this in the documentation).

3. What did you see instead (Required)

a: 1
b: 1

4. What is your KCL components version? (Required)

kcl version 0.11.1

This resulted in a very nasty bug in a much more complex piece of code. For now I worked around this by calling my main function from the bottom of the file, similar to how it is often done in Python which so far seems to avoid this issue while still being able to write my code from top to bottom. So the code (a crossplane kcl function) will then look more like this

main = lambda {
    [wrappedIdentity(1), identity(2)]
}

wrappedIdentity = lambda item {
    identity(item)
}

identity = lambda item {
    item
}

items = main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions