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.

Can someone explain new box()?

EvanReeseEvanReese Member, Mentor Posts: 2,186 ✭✭✭✭✭

I see new box() used in the std and other places by people who are better at coding than me, but still haven't wrapped my head around it or when I'd want to use it. I've read the explanation here in the FS documentation, and AI says to use Box Types "When you need to wrap a single value and potentially add additional metadata or behavior or for implementing generic programming patterns", but I'm still not clear on it. Anyone care to explain it a bit or link me to a good primer?

Evan Reese

Best Answer

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    edited November 27 Answer ✓

    Boxes are a way around the issue that FeatureScript values are generally unchangeable — if you have:

    var x = { a : 12 };
    myFunction(x);
    println(x);
    

    You'll always see {a : 12} no matter what myFunction does. (It does not work this way in Java or JavaScript, for example).

    If you want a function to be able to change an argument, you need to put it in a box, like this:

    var x = new box({ a : 12 });
    myFunction(x);
    println(x[]);
    
    …
    // In myFunction:
    x[] = 3;
    

    Then this code will print 3 because myFunction changed "what is in the box".

    This can lead to useful patterns like the pseudo random number generator I posted here:

    Hope this helps.

    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc

Answers

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,215
    edited November 27 Answer ✓

    Boxes are a way around the issue that FeatureScript values are generally unchangeable — if you have:

    var x = { a : 12 };
    myFunction(x);
    println(x);
    

    You'll always see {a : 12} no matter what myFunction does. (It does not work this way in Java or JavaScript, for example).

    If you want a function to be able to change an argument, you need to put it in a box, like this:

    var x = new box({ a : 12 });
    myFunction(x);
    println(x[]);
    
    …
    // In myFunction:
    x[] = 3;
    

    Then this code will print 3 because myFunction changed "what is in the box".

    This can lead to useful patterns like the pseudo random number generator I posted here:

    Hope this helps.

    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • EvanReeseEvanReese Member, Mentor Posts: 2,186 ✭✭✭✭✭

    oooh! Thanks Ilya, that made it click. Lots of possibilities.

    Evan Reese
Sign In or Register to comment.