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

Implicit casting does compile without errors #20

Open
porky11 opened this issue Aug 3, 2017 · 4 comments
Open

Implicit casting does compile without errors #20

porky11 opened this issue Aug 3, 2017 · 4 comments

Comments

@porky11
Copy link

porky11 commented Aug 3, 2017

When assigning a type to another type without explicitely casting, I would expect errors, but there aren't.
For example here:

#include <vsr/vsr.h>
using namespace std;
using namespace vsr;

using ega = vsr::algebra<vsr::metric<3>, int>;

int main() {
    using vector = ega::types::vector;
    auto e1 = ega::types::vector(1,0,0);
    auto e2 = ega::types::vector(0,1,0);
    vector e1e2 = e1*e2;  //this will create a rotor. When assigning to a vector, it will become zero.
    e1e2.print();
}
@wolftype
Copy link
Owner

wolftype commented Aug 4, 2017

hmm, this was intentional but I can see how it can be considered unexpected. can you argue the case for why it is important that the above code produce an error?

@porky11
Copy link
Author

porky11 commented Aug 4, 2017

When adding type annotations, the main reason probably would be to add security for the code.
So when I think, multiplying two vectors gives a bivector, then I write bivector instead of auto, so I'm sure, I have a bivector before, and can use it afterwards.
Or another example could be, that I confuse * and +. Then I add type annotatoins, and wonder, why my vector is zero.
Especially in complex calculations it may be nice to be able to ensure, that all types are correct, I wrote down.
But maybe how you have done it is also ok, since in c++ also other numbers like floats get implicitely casted.
I prefer, when at least casts, where you lose information, are explicite, as it's also done in most modern languages (e.g. rust and dale).

@porky11
Copy link
Author

porky11 commented Aug 8, 2017

Also for addition it should be required to have the same type by default. You almost never would add together n-vectors of different grade.

@wolftype
Copy link
Owner

You make good points about the virtues of explicit casting. I need to investigate whether the templatizaion technique I employed would allow me to keep generic casting and lose implicit assignment. As for adding n-vectors of different grade, I'm not convinced by your last statement. For instance, in the case of a scalar + a bivector (e.g. simple rotor) it is quite useful to be able to specify the type as decltype ( 1 + B())

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

No branches or pull requests

2 participants