Skip to content

string length type inconsistency #1338

Closed

Description

@asmaloney

Godot version

4.2

godot-cpp version

4.2-stable

System information

macOS (but it's irrelevant)

Issue description

In the test code:

String Example::test_string_resize(String p_string) const {
	int orig_len = p_string.length();
	p_string.resize(orig_len + 3);
...

String::length() returns an int64_t, but String::resize() only takes an int:

Error resize(int p_size);

When dealing with string sizes, the type should be consistent throughout the class. In fact I would recommend creating a type for it so it's obvious when we're dealing with lengths. Something like:

using StringLen = int64_t;

StringLen length() const;
Error resize(StringLen p_size);

Steps to reproduce

(It's an API bug.)

Minimal reproduction project

(It's in the test code: test/src/example.cpp - Example::test_string_resize)

Activity

  1. added
    enhancement
    on Dec 17, 2023
  2. AThousandShips commented on Dec 17, 2023

    @AThousandShips
    Member

    I'd say length should return int here as well, to match the API, this is likely due to a limitation in the implementation (resize is actually added manually in the bindings generator). String::length in the engine returns int, as does the godot-cpp CharStringT

    The underlying data container only handles 32 bit lengths, so the change here shouldn't be in the argument, but the return type, but in either case I'd say this breaks compatibility

    Generally I think this could be solved if the arguments of Variant types employed the meta data information, so it correctly conveyed the type and signedness of the arguments

  3. dsnopek commented on Dec 22, 2023

    @dsnopek
    Collaborator

    I'd say length should return int here as well, to match the API, this is likely due to a limitation in the implementation (resize is actually added manually in the bindings generator). String::length in the engine returns int, as does the godot-cpp CharStringT
    [...]
    Generally I think this could be solved if the arguments of Variant types employed the meta data information, so it correctly conveyed the type and signedness of the arguments

    I agree!

    Godot does set "meta" in the JSON to "int32" or "int64" for some arguments and return types (for example, AnimatedSprite3D::set_frame() and AnimatedSprite3D::get_frame()), but it doesn't in the case of String::length() for some reason.

    Perhaps it's because the variant types are handled a little differently than the ClassDB types?

  4. AThousandShips commented on Dec 22, 2023

    @AThousandShips
    Member

    The variant types don't get any meta at all, this also affects the documentation currently, don't know if work is being done on fixing this, it's due to a different binding methodology

  5. dsnopek commented on Jan 24, 2024

    @dsnopek
    Collaborator

    Fixed by PR #1357

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaks compatdiscussionenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions