Quantcast
Channel: Questions in topic: "policy"
Viewing all articles
Browse latest Browse all 64

Writing Policy class for handling Vector2s and 3s.

$
0
0
I'm finding I have to write a lot of similar code in certain situations for Vector2s and 3s. So, I figured write a generic class that takes a VectorPolicy and does calculations with the appropriate type. The only issue is Vector2s and 3s don't have interfaces backing them. So, I decided to make a policy class that would call the appropriate methods on each. Like so: public interface IVectorPolicy { T Add(T a, T b); float Dot(T lhs, T rhs); } That I'd use like so: public class Test where T : IVectorPolicy, new() { readonly T vectorPolicy; public Test() { vectorPolicy = new T(); } public Foo(G vector1, G vector2) { G g = vectorPolicy.Add(vector1, vector2); float dotProduct = vectorPolicy.Dot(g, vector1); } } But this causes other issues with G not being known at compile time. As far as I'm aware the compiler cannot figure out the Type of a Generic Type being passed to another Generic Type at compile time. Anyone have any idea how I could work around this?

Viewing all articles
Browse latest Browse all 64

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>