BehaviorTree.CPP icon indicating copy to clipboard operation
BehaviorTree.CPP copied to clipboard

Add support to concat strings and numbers in scripting

Open bochen87 opened this issue 1 year ago • 3 comments

Solves #766

bochen87 avatar Feb 13 '24 12:02 bochen87

The potential issue I see with this is that there are two contraddicting use case:

  1. Someone may want to concatenate string with numbers, so for instance
"path_" + 4 -> "path_4"
  1. Someone may want strings to be interpreted as numbers
"3" + 4 -> 7

What would be probably "wrong" is:

"3" + 4 -> "37"

So, the only sane way to do this, in my opinion is including functions to_number() and to_string():

"path" + to_string(4) -> "path_4"
to_number("3") + 4 -> 37

This is considerably more work, but the only way to avoid technical debt from being created, in my opinion.

facontidavide avatar Feb 13 '24 15:02 facontidavide

The potential issue I see with this is that there are two contraddicting use case:

  1. Someone may want to concatenate string with numbers, so for instance
"path_" + 4 -> "path_4"
  1. Someone may want strings to be interpreted as numbers
"3" + 4 -> 7

What would be probably "wrong" is:

"3" + 4 -> "37"

So, the only sane way to do this, in my opinion is including functions to_number() and to_string():

"path" + to_string(4) -> "path_4"
to_number("3") + 4 -> 37

This is considerably more work, but the only way to avoid technical debt from being created, in my opinion.

It would be nice, but I guess quite a large amount of work to make it work properly, as your outlined approach also has some potential pitfalls, i.e. "3,14" vs "3.14" vs "8'000". However, in the meantime, this could still be a good enough intermediate solution as it does at least allow for the usecase 1 to be solved.

bochen87 avatar Feb 16 '24 08:02 bochen87

However, in the meantime, this could still be a good enough intermediate solution as it does at least allow for the usecase 1 to be solved.

Sorry, not adding this feature yet. I can't afford to have side effects that haven't been considered carefully

facontidavide avatar Feb 22 '24 11:02 facontidavide

Would a better solution for this be: to add a new operator specifically for string concatenation? Similar to how it is done in Lua. e.g.

value := 10
message := "The value is " .. value

would result in The value is 10

I can see this avoiding any issues with people wanting to use strings converted to numbers as it is explicitly for string concatenation.

EDIT: I've made a new PR #802 that works for my needs but can add some changes if required.

seanng-1 avatar Mar 28 '24 01:03 seanng-1

closing in favor of https://github.com/BehaviorTree/BehaviorTree.CPP/pull/802

facontidavide avatar Apr 12 '24 12:04 facontidavide