afw-org / afw

Adaptive Framework
https://afw.tools
MIT License
4 stars 1 forks source link

Possible change to string add method #29

Closed mike000000000 closed 1 year ago

mike000000000 commented 1 year ago

The string add is used by the "+" operator. We decided to generally not do auto cast and we decided to not have a special "concatenate" operator. The main reason for not doing auto cast in the string add is because 2 + '2' will yield "22", which can be an unintended/unexpected result. For strings, afw also has the concat() function that does concatenate of the string values of all parameters, including the first.

We've probably already discussed this as a possibility before, but I propose that we change the string add to convert all except the first parameter to it's string value. This change is only for the '+' operator and no other functions. Here are some results:

expression ECMAScript result AFW result
2 + '2' '22' error because the integer add operator only accepts integer parameters.
1 + 1 + '2' '22' error because the integer add operator only accepts integer parameters.
'2' + 2 '22' '22' because intent to be string indicated up front.

This last example seems like a useful pattern.

One common ways some languages force a string concatenation when the concatenated value are a mixture of string and numbers is to start it with an empty string ""+. This will work if we make this change.

mike000000000 commented 1 year ago

@JeremyGrieshop I'm finished with this unless you find something I need to fix.