Strange behavior when implementing GH_Goo<T>'s CastTo<Q>() and ToString()

The plugin we’re developing leverages a variant data type to enable meta data structures. While implementing GH_Goo<T> for custom grasshopper nodes, we encountered different behaviors between the text node and the rest of the standard datatype nodes (integer, number, geometry, etc) For the major of the nodes, a correct implementation of CastTo<Q>(ref Q target) allows the variant data to be correctly cast to the specified type; however, the Text data node behaves differently. Instead of using CastTo<Q>, the Text node calls ToString(). This wouldn’t be a huge issue, except that the panel component also uses ToString(). Since there’s other metadata in our data structure, it’s very useful to leverage the panel to have visibility into everything that is contained in our data structure. So changing the behavior of ToString() based upon the datatype of the variant isn’t desirable (it also feels bad from a C# standpoint). I’ve used breakpoints in the code to verify that the Text node is calling ToString() instead of CastTo<Q>().

I’ve attached a screen shot of the behavior for reference. In this example you can see that the string value produces the same result whether it’s directly fed to a panel or through a Text node while an integer value get’s cast correctly when run through an integer node first.

I know you’re hard at work with Grasshopper2, but I was curious if this was intended behavior? If it was, I’d like to request that Grasshopper2 not use ToString() as a substitute for casting an object to string.


1 Like