Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Why can't we use + to concatenate strings?
adamohern
Member, OS Professional Posts: 216 PRO
It took quite a while to figure out that tilde (~) is used for string concatenation in FS. I'm sure there's a good reason why you chose to use a special operator, but I wonder what that might be, and why tilde was chosen specifically? I've never encountered that in any other language.
1
Comments
You have to wrap the concatenation in parentheses or it fails. Logically speaking, if A + B works, and A + C works, I would expect for A + B + C to work.
id + ("splitPart"~i) is doing two different things: it's adding a level to the id hierarchy and that level is labeled by a concatenated string. id + i + "splitPart" would work and add two levels to the hierarchy.
It's not great that it's not consistent with other languages, but that was the tradeoff for an easy and unambiguous way to turn any value into a string.
You are right -- I forgot -- I did get the tilde operator from D (they have similar reasoning on this).
I just found the id declaration thing to be really confusing. It works, and I'm using them without a problem, but it's all very fuzzy.
Would you happen to have a convenient link handy that explains what the heck id was using + for? @ilya_baran
mentioned something about an "id hierarchy" where I assumed what we were doing was simply assigning a unique id "string" that is composed of the id string passed in through the defineFeature() method concatenated to some arbitrary text. Upon reading more on for loops where it's best practice to append the index to the string literal using tilde, is where that nuance starts to rear its ugly head and google led me to this thread in the forums.
That link is probably as good an explanation as we have. You can also look in std itself (https://cad.onshape.com/documents/12312312345abcabcabcdeff/v/ea4cda9e1faa7aa24e82d28d/e/1ccf6e5321844da29ab72f2b)
to see what exactly we do, but it doesn't tell you too much about why.
The string concatenation operator (~) converts any non-string value to a string (absolutely anything can be converted to a string by "" ~ value)
IR for AS/NZS 1100
That works but using `toString(value)` is somewhat more declarative if you are not concatenating onto something.
-----------
@tom_auger
One thing that that description of ids does not mention is the power that comes from id hierarchy: