brendanzab / algebra

Abstract algebra for Rust (still very much a WIP!)
Apache License 2.0
60 stars 9 forks source link

Wrapper structs to use a richer structure as a lesser one #19

Open milibopp opened 9 years ago

milibopp commented 9 years ago

Given e.g. a group, there are two sensible ways to view it as a magma. In situations like this, it is not possible to express this relation via trait inheritance without parametrizing over operator symbols. See the discussion in #18 for comparison. Wrapper structs essentially provide the same feature without parametrization.

They could be implemented like this:

struct GroupAsDivisiveMagma<G>(G);
struct GroupAsDivisiveMagmaByRef<'a, G>(&'a G);

Both these variants have their downsides. The first one allows one to easily implement the magma trait but as it consumes its value, it is not practical for non-copy types (think of the group formed by large invertible matrices with their multiplication).

The second one serves as a view of the group but it can not satisfy the function signature of the magma trait making it impossible to use with functionality implemented for magmas.

NOTE: the discussion to decide between this and #18 takes place at #18.

brendanzab commented 9 years ago

I was about to ask about references. This is an added layer of complexity that is present in Rust as and not in other languages like Scala or Haskell. A big part of Rust's value proposition is being able to choose between passing by value, or passing by reference. It would be great if we could figure out a nice way to expose this in our API without causing too much complexity.