How to make multiple constructors for a class?

Hello!

I would like to have multiple ways of instanciating a class, like you would overload a constructor in C#.
What is the best/clean way to define multiple contructors (i.e. multiple Create operations with different signatures) in Gamma?

It seems the definition patch won’t let me have multiple operations named “Create” so I’m working around that with versions - for instance “Create (Empty)”, “Create (Spread)”, “Create (MutableArray)”…

which seems to work fine, but once in the parent patch there is still an input on the “Create (WhateverVersion)” nodes which leads me to think there are not recognized as a proper constructor, but just a regular operation, which (as far as I understand) will create a new instance if no reference is input.

Is there any drawback to proceeding like this, or is there a cleaner way?

Also, instead of creating a dedicated “Create (Empty)” version, is there anything wrong doing just this in the parent patch?

Thanks so much for your insights!
++
T

you can make convenient operation nodes that combine a create and a init operation. not a real overload for create (because iirc. that is not possible in vl) and a bit more cumbersome, but on the patching side it should do.

4 Likes

@sebl that’s super smart!

Thanks for the tip and demo patch 🖤

And is there something wrong in doing this when you want a basic “Create without parameters” instance?

I’d argue doing things explicitly is always better :-)