Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance methods #7

Open
ChargeProduction opened this issue Jan 18, 2017 · 4 comments
Open

Instance methods #7

ChargeProduction opened this issue Jan 18, 2017 · 4 comments

Comments

@ChargeProduction
Copy link

Sometimes a simple position struct has to be updated. There is actually no need to allocate a new object and let the old one be garbage on the heap. Also, this method of using memory is much much faster than creating a new instance from , for example, two multiplied struct.

I would aprreciate instance methods like:

// Somewhere in vec3.cs

public void Load(vec3 vec){
    this.x = vec.x;
    this.y = vec.y;    
    this.z = vec.z;
}

public void Mult(vec3 vec){
    this.x *= vec.x;
    this.y *= vec.y;    
    this.z *= vec.z;
}

An example how I would use it with matrices:

// In Camera.cs

public void Update(){
    viewMat.Load(dmat4.Identity);
    viewMat.Translate(position);
    viewMat.Mul(rotation.ToMat4);
    ...
}
@Philip-Trettner
Copy link
Owner

I really dislike this for syntactical reasons but I agree that it has performance benefits.

How about this idea:
All those methods are implemented as Extension Methods inside a new namespace (e.g. GlmSharp.PerformanceExtensions).
They will not clutter the interface unless you explicitly state using GlmSharp.PerformanceExtensions; in the beginning.

Does that work?

@ChargeProduction
Copy link
Author

I`m not sure if its works.

You have to use the this keyword in your extension methods. It will copy the struct so you are not able to edit the fields =/ Thats what I experienced.

It works with the ref keyword. But then Its not an extension. Also you have to make sure you never assign an other struct to the ref` parameter before the assigning values. I can explain it in detail if needed.

@Philip-Trettner
Copy link
Owner

Right that makes sense.

Hm I have to think about it a bit more because I really don't like to clutter the interface unnecessarily.

@ChargeProduction
Copy link
Author

I just have tried some approaches. But there is just no way to create an extension with this ref or some kind of other access...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants