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

Added forced casting of int values ​​to float for float-typed fields #216

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from

Conversation

EsipovPA
Copy link

@EsipovPA EsipovPA commented Sep 10, 2024

Added casting int to floats for float-typed fields in messages
Added test for new feature

Closes this issue

Copy link
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to say that personally, I'm not a fan. int and float are not the same thing, and thus I think any conversion like this should be up to the user to explicitly do. I'd like to hear opinions from @ros2/team, though.

@EsipovPA
Copy link
Author

EsipovPA commented Sep 12, 2024

Hello @clalancette!

Thanks for the quick reply.

It seems to me that this is a case of a widening conversion, that should be an option for these types of fields. In case of C++ code, the int values would be casted to float automatically, when passed to = operator. The idea behind this code is to make possible something like this for python code. Also it just makes python code of message initialization simpler and easier to read and write, as I see it.

For example, instead of something like this

msg = Msg(check_fields=True, value=float(1))

or

msg = Msg(check_fields=True, value=1.)

user should be able to write just

msg = Msg(check_fields=True, value=1)

and get the float value without raised exceptions.

Still confused about the failed test though. Checking

@clalancette
Copy link
Contributor

It seems to me that this is a case of a widening conversion, that should be an option for these types of fields. In case of C++ code, the int values would be casted to float automatically, when passed to = operator. The idea behind this code is to make possible something like this for python code. Also it just makes python code of message initialization simpler and easier to read and write, as I see it.

Yeah, I understand what you are trying to do. I still don't think it is a good idea, even if C++ allows it (there is nothing we can do about that, it is part of the language).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integers should be accepted as floats
2 participants