Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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?

EvanReeseEvanReese Member, Mentor Posts: 2,188 ✭✭✭✭✭
edited January 2023 in FeatureScript
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

Best Answers

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,216
    Answer ✓
    Such 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 Inc
  • lanalana Onshape Employees Posts: 711
    Answer ✓
    @Evan_Reese
    In C/C++ you can do this with a macro. I don't think FS language development will go that way. 

Answers

  • tim_hesstim_hess Member Posts: 46 ✭✭
    Is it just a typo in "variableNameToString"? It looks like you're missing an "l" in variable. 
  • EvanReeseEvanReese Member, Mentor Posts: 2,188 ✭✭✭✭✭
    Good catch, but that's not it. variableNameToString() is my made up example function to stand in for what I want, unless it actually exists and I didn't think to check, haha
    Evan Reese
  • tim_hesstim_hess Member Posts: 46 ✭✭
     :D  

    Creating new functions like that seems like a good use-case for ChatGTP.
  • EvanReeseEvanReese Member, Mentor Posts: 2,188 ✭✭✭✭✭
    lol. I just tried and it's overloaded at the moment.
    Evan Reese
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,216
    Answer ✓
    Such 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 Inc
  • EvanReeseEvanReese Member, Mentor Posts: 2,188 ✭✭✭✭✭
    @ilya_baran
    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.
    Evan Reese
  • mahirmahir Member, Developers Posts: 1,309 ✭✭✭✭✭
    If it's important to be able to reference an actual name or label for your variable, you could create a map x[n,1] where x[i,0] is the name and x[i,1] is the value. 
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,071 PRO
    edited January 2023
    How would bindings work on a map? Would the map's scope be assigned to all it's associated pairs? I think scope would be easier if it were just assignable vs. where it was defined.

    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.


  • lanalana Onshape Employees Posts: 711
    Answer ✓
    @Evan_Reese
    In C/C++ you can do this with a macro. I don't think FS language development will go that way. 
  • EvanReeseEvanReese Member, Mentor Posts: 2,188 ✭✭✭✭✭
    No worries. It's not critical for me. I just was wondering.
    Evan Reese
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,071 PRO
    edited January 2023
    You could save your variables in the definition map that's included in almost every feature script example.

    //2 ways to create a variable
    Var test=5*meter
    definition.test=5*meter



    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.


Sign In or Register to comment.