Godot: Bools are implicitly cast to ints

Created on 10 Jul 2020  路  8Comments  路  Source: godotengine/godot

Godot version:

3.2.2

Issue description:

func _ready() -> void:
    test(true)

func test(arg: int):
    print(arg)

This runs without errors and prints 1.

bug gdscript

Most helpful comment

@ThakeeNathees I'd say the only implicit cast allowed should be int -> float (like when using declare(strict_types = 1); in PHP). That said, this would likely break backwards compatibility.

All 8 comments

the boolean is implicitly converted to integer (behaves like a derived instance) we can disable the conversion for only builtin types here :https://github.com/godotengine/godot/blob/a474926aca8a5da810f8cf546902c6da7286b254/modules/gdscript/gdscript_function.h#L72
but that also makes int to float implicit conversions also invalid, what's the expected behavior?
@KoBeWi

@ThakeeNathees I'd say the only implicit cast allowed should be int -> float (like when using declare(strict_types = 1); in PHP). That said, this would likely break backwards compatibility.

I'd say the only implicit cast allowed should be int -> float

float -> int should stay valid too and throw warning like it does now.

should bool -> float, int -> bool, ... be invalid or it just makes a warning ??

bool -> float and int -> bool should be invalid.

But what's wrong with int -> bool and float -> bool? This convertion is quite useful and can compact the code without sacrifizing readability, especially when used in if statements. Or will it still work with ifs?

Or will it still work with ifs?

Ifs are fine. We are talking about method arguments.

Note that the following code will no longer work:

func _item_selected(idx: int) -> void:
    get_tree().debug_collisions_hint = idx & 0b01
    get_tree().debug_navigation_hint = idx & 0b10

Explicit conversion will be required:

func _item_selected(idx: int) -> void:
    get_tree().debug_collisions_hint = bool(idx & 0b01)
    get_tree().debug_navigation_hint = bool(idx & 0b10)
Was this page helpful?
0 / 5 - 0 ratings