wolftype / versor

Versor Geometric Algebra Library
wolftype.github.io/versor/devel/html/
290 stars 47 forks source link

Implicit casting does compile without errors #20

Open porky11 opened 7 years ago

porky11 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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())