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.
get a variable name as a string?
EvanReese
Member, Mentor Posts: 2,188 ✭✭✭✭✭
if I have a variable in a feature, and want to print the name of the variable, how do I do that? For example
var test = 5*meter; print(variabeNameToString(test));should print "test", not "5*meter".
Evan Reese
0
Best Answers
-
ilya_baran Onshape Employees, Developers, HDM Posts: 1,216Such a function cannot be written because no function f can distinguish being called as f(5 * meter) and f(test) where test == 5 * meter. This is true for the majority of programming languages.
But what are you actually trying to do? I can't think of a use case for what you're asking for...Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc1 -
lana Onshape Employees Posts: 711@Evan_Reese
In C/C++ you can do this with a macro. I don't think FS language development will go that way.1
Answers
Creating new functions like that seems like a good use-case for ChatGTP.
But what are you actually trying to do? I can't think of a use case for what you're asking for...
I figured it might be something like that, and I'm sure it's for the best. I honestly don't recall exactly what I was trying to do when I asked the question, but it was something to do with automatically printing information more readably to debug things, without having to write all the variables names out. I guess I could put each variable in a map and use the keys, if I need to do something like that. It was also just partly curiosity about whether it's possible.
I've been wondering if you do a lot of API development, wouldn't it make more sense to just use maps? You know, let's just get rid of variables. One thing that is really nice is keeping the same object moving from client to server to onshape and then back to server to client. It seems like a good way to write cloud applications.
Look at parseJson, does it make sense to pass the cloud object around which would be a map in featurescript. Why doesn't feature script have a stringfyMap function to turn a map into JSON. Can we just we just assume values are in meters and do away with value with units in JSON or to keep to the JSON standard.
I like the idea of using maps for data inside feature script.
In C/C++ you can do this with a macro. I don't think FS language development will go that way.
Then you have access to your variables using keys & values. I see that FS supports keys. I know I can get to "test" using js.
Just for fun, I ran toString(definition) through a jsLinter:
It's close but not close enough.
It does JSON.stringify your "test" variable properly
On second thought, the above is wrong.
I'm thinking an OS map to json is going to be harder than I originally thought.