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

[BUG] Incorrect Boolean Casting #398

Open
andre0xFF opened this issue Aug 9, 2024 · 4 comments
Open

[BUG] Incorrect Boolean Casting #398

andre0xFF opened this issue Aug 9, 2024 · 4 comments
Labels
bug Something isn't working compiler

Comments

@andre0xFF
Copy link

Describe the bug
When casting a string value "true" to a boolean, the resulting boolean value matches the incorrect if condition. This issue occurs both when executing the script directly and when compiling it to bash. Amber version: amber 0.3.4-alpha

To Reproduce
Script:

main(args) {
    let foo = "true" as Bool

    echo "Foo is {foo}"

    if foo == true {
        echo "it's true"
    }

    if foo == false {
        echo "it's false"
    }
}

Execution:

❯ amber test.ab
Foo is true
it's false

Compiled to bash:

❯ amber test.ab test.sh
❯ ./test.sh
Foo is true
it's false

Expected behavior
The output should be:

Foo is true
it's true

Additional context
The issue seems to be related to the incorrect casting of string values to boolean. The string "true" is casted to a boolean but matches the false condition.

@andre0xFF andre0xFF added the bug Something isn't working label Aug 9, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Aug 9, 2024
@andre0xFF
Copy link
Author

Possible workaround by running the condition in a bash command like let service_url = $if [ \"\$IS_LOCAL\" = \"false\" ]; then echo \"foo.bar:8000\"; else echo \"localhost:8000\"; fi$?

@Mte90
Copy link
Member

Mte90 commented Aug 9, 2024

Probably we need a better conversion for this type internally.

PS: Amber when execute the code compiles it and run a bash process so it is the same.

@Mte90 Mte90 added the compiler label Aug 9, 2024
@Ph0enixKM
Copy link
Member

Is it a valid cast though? The cast from Text to Number is marked as "absurd". The same should apply to Text->Bool.

  • A warning should be appeared that this is an "absurd cast" just like when casting Text to Num
  • The normal behaviour should check if the text == "1" and be a bash representation of "1" for true and "0" for false. This should be for casting anything to Bool

@KrosFire
Copy link
Member

Is it a valid cast though? The cast from Text to Number is marked as "absurd". The same should apply to Text->Bool.

  • A warning should be appeared that this is an "absurd cast" just like when casting Text to Num
  • The normal behaviour should check if the text == "1" and be a bash representation of "1" for true and "0" for false. This should be for casting anything to Bool

I agree that "true" as Bool shouldn't convert a value to boolean true. If you want a Boolean from text, you should just compare texts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler
Projects
None yet
Development

No branches or pull requests

4 participants