Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected type inference behavior with lambda functions and schema instantiation #1580

Closed
suin opened this issue Aug 18, 2024 · 2 comments
Closed
Labels
bug Something isn't working lsp resolver runtime Issues or PRs related to kcl runtime including value and value opertions

Comments

@suin
Copy link

suin commented Aug 18, 2024

First of all, I'd like to express my gratitude to the KCL development team for creating such a powerful and innovative language. KCL has greatly improved our configuration management processes, and we appreciate your continuous efforts to enhance the language.

Bug Report

1. Minimal reproduce step (Required)

  1. Create a new KCL file with the following content:
schema Container:
    item: Item

schema Item:
    name: str

schema Entry:
    title: str

schema ItemLike:
    name: str

entry_to_item = lambda inputs: Entry -> Item {
    Item {name = inputs.title}
}

item_like_to_item = lambda inputs: ItemLike -> Item {
    Item {name = inputs.name}
}

# This compiles successfully
Container {
    item = {name = "foo"}
}

# This also compiles successfully
Container {
    item = Item {name = "foo"}
}

# This causes a Compile Error
Container {item = entry_to_item({title = "foo"})}

# This compiles successfully
Container {item = entry_to_item(Entry {title = "foo"})}

# This compiles successfully
Container {item = item_like_to_item({name = "foo"})}
  1. Try to compile the file.

Alternatively, you can reproduce this issue using the KCL Playground:
http://101.42.3.224/-/play/index.html?id=efe9b08292ef976de5ffda37dbbc800d7ac11b8b

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

I expected all of the Container instantiations to compile successfully, especially the one using entry_to_item({title = "foo"}). Since entry_to_item is defined as a lambda that takes an Entry and returns an Item, and Container.item is of type Item, this should be a valid assignment.

3. What did you see instead (Required)

The compilation fails with an error message for the line:

Container {item = entry_to_item({title = "foo"})}

The error message states:

Cannot add member 'title' to schema 'Item'

This error is unexpected because:

  1. The lambda entry_to_item is correctly typed to accept an Entry and return an Item.
  2. The inline object {title = "foo"} matches the structure of Entry.
  3. Similar constructions with item_like_to_item work correctly.

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

KCL version: 0.9.5

Additional Notes:

  • This behavior suggests an inconsistency in type inference or structural typing implementation.
  • The error can be avoided by explicitly typing the argument: entry_to_item(Entry {title = "foo"}), but this shouldn't be necessary given the lambda's type signature.
  • This inconsistency makes the language behavior less predictable and may lead to confusion for users.
  • The issue can be reproduced in the KCL Playground, which makes it easier for the development team to investigate.
@Peefy
Copy link
Contributor

Peefy commented Aug 19, 2024

Related Issue: #1527 This issue has been fixed in #1531 and #1529, and released in v0.10.0-beta.1

@Peefy Peefy added bug Something isn't working lsp resolver runtime Issues or PRs related to kcl runtime including value and value opertions labels Aug 19, 2024
@Peefy Peefy added this to the v0.10.0 Release milestone Aug 19, 2024
@Peefy
Copy link
Contributor

Peefy commented Aug 20, 2024

Closed by #1531 and #1529, released in KCL v0.10.0-beta.1

@Peefy Peefy closed this as completed Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lsp resolver runtime Issues or PRs related to kcl runtime including value and value opertions
Projects
None yet
Development

No branches or pull requests

2 participants