✅ 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
HtmlCompiler
HtmlCompiler8mo ago
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?
sibber
sibber8mo ago
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
TheHelpfulHelper
TheHelpfulHelperOP8mo ago
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...
SleepWellPupper
SleepWellPupper8mo ago
static class VectorExtensions
{
public static LibB.Vector3 ToLibB(this LibA.Vector3 vector) => new LibB.Vector3(x: vector.X, y: vector.Y, z: vector.Z);
\\the inverse also
}
static class VectorExtensions
{
public static LibB.Vector3 ToLibB(this LibA.Vector3 vector) => new LibB.Vector3(x: vector.X, y: vector.Y, z: vector.Z);
\\the inverse also
}
Where do you see a need for more extension methods? I believe these two methods were what @sibber 🇵🇸 had in mind, no?
sibber
sibber8mo ago
i guess you could use a mapper like mapperly yes those are extension methods
SleepWellPupper
SleepWellPupper8mo ago
The point was:
Where do you see a need for more extension methods?
sibber
sibber8mo ago
ah well to convert them? im not sure what you mean
SleepWellPupper
SleepWellPupper8mo ago
The question was aimed at @TheHelpfulHelper; they were concerned about using extension methods as there
would potentially be a lot of extension methods
I 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
TheHelpfulHelper
TheHelpfulHelperOP8mo ago
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
SleepWellPupper
SleepWellPupper8mo ago
Hm yeah if there's more types than just vec3 I suppose that could get messy.
sibber
sibber8mo ago
$close
MODiX
MODiX8mo ago
If you have no further questions, please use the /close command to mark the thread as answered
Want results from more Discord servers?
Add your server