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.

Featurescript: type checking

jrs_spedleyjrs_spedley Member Posts: 70 ✭✭
Just a quickie, I can find an isInteger function and an isReal function (with bounds) but it seems they are more for 'features' rather than programming.  Is there a way when parsing an array to check what data type each element is, i.e. isBox, isArray, isString etc?

Comments

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,279
    edited April 10
    @jrs_spedley

    You can use `is <type>` for this.

    Example:
    for (var item in someArray)
    {
        if (item is box)
        {
            ...
        }
        else if (item is number)
        {
            ...
        }
        else if (item is ValueWithUnits)
        {
            ...
        }
    }

    Same syntax as the `is` syntax for typechecking function parameters:
    function doSomething(context is Context, id is Id, definition is map)
    {
        ...
    }

    Jake Rosenfeld - Modeling Team
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,279
    A word of caution as well.  Types have hierarchy and all non-builtin types (UpperCaseTypes) are built on top of builtin types (lowercasetypes).  Two specific examples are that a Vector is actually just a special array, and a ValueWithUnits is just a special map.

    So if you do:
    if (item is Vector)
    {
        ...
    }
    else if (item is array)
    {
        ...
    }
    or
    if (item is map)
    {
        ...
    }
    else if (item is ValueWithUnits)
    {
        ...
    }
    you will never actually hit the second block of execution.

    To check what builtin type a certain Type is based on you can search the standard library for `export type <TypeYouAreCuriousAbout>`:


    Here you can see that a Vector is just an array with nonzero size.
    Jake Rosenfeld - Modeling Team
  • jrs_spedleyjrs_spedley Member Posts: 70 ✭✭
    Doh, do I feel dumb now!  That didn't even cross my mind, I just assumed it was for specifying parameters rather than an actual function.

    Thanks for super swift response.  I'll go and change everything back now as I've just changed all my arrays into boxes as a work around!  :)
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,279
    You're welcome!
    Jake Rosenfeld - Modeling Team
Sign In or Register to comment.