✅ Conversions of types between two libraries
So I have two libraries (A and B) which both have their own Vector3 structs and in my project I want to pass them back and forth, because the libraries offer different functionality that uses the Vector3s.
Do I have to create an intermediary representation that implements implicit conversions in both directions or is there a better way?
A round-trip would look like this currently:
LibA.Vector3 > MyCode.Vector3 > LibB.Vector3 > MyCode.Vector3 > LibA.Vector3
Not just does this create a lot of boiler-platey code if I have to do it for all of the other Mathematical structs (Vector2, Vector4, Quaternion, Matrix3x3, Matrix4x4 etc.) but it also seems kinda wasteful, if it actually has to allocate memory for each cast which is pretty much immediately discarded again...
12 Replies
if it's floats and a struct it should be small enough to stay on the stack and not request heap memory
...but why offering different functionalities requires creating a custom vector type?
well you cant implicitly convert two types in one statement
youd still have to explicitly cast to the middle man type
so id just do extension methods
until we get extensions
Dont ask me why these libraries thought they needed to create their own Vector structs, something something memory layout I think or I dont really know. All I know is that I need to use the libraries and I cant edit them.
Is there really no better way? That would potentially be a lot of extension methods...
Where do you see a need for more extension methods?
I believe these two methods were what @sibber 🇵🇸 had in mind, no?
i guess you could use a mapper
like mapperly
yes those are extension methods
The point was:
Where do you see a need for more extension methods?
ah
well to convert them? im not sure what you mean
The question was aimed at @TheHelpfulHelper; they were concerned about using extension methods as there
would potentially be a lot of extension methodsI don't know the entire library use case context but from my perspective two conversion extension methods should suffice. I was curious as to what other methods they had in mind is all
I just meant that two methods (back and forth) per common struct/class could potentially be a lot if you have a lot of them.
I suppose it is still the best solution, but seems rather unsatisfying
Hm yeah if there's more types than just vec3 I suppose that could get messy.
$close
If you have no further questions, please use the /close command to mark the thread as answered