Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created March 5, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoelzro/06ebf55b6b679ce4cb16 to your computer and use it in GitHub Desktop.
Save hoelzro/06ebf55b6b679ce4cb16 to your computer and use it in GitHub Desktop.
Real world use case for COERCE
Let's say there are two Matrix classes: Matrix::PP and Matrix::Blas. They are implemented independently of each other, but have a mostly compatible interface.
Now let's suppose I am using a module that returns a Matrix::PP ($pp-matrix), and I want to use it to perform some calcualtions. Pure-Perl 6 matrix calculations will be slower than using Matrix::Blas (which is backed by libblas), so I want to convert between the two.
I can't call $pp-matrix.Matrix::Blas, because Matrix::PP doesn't know about Matrix::Blas. I also can't do Matrix::Blas($pp-matrix), because even if Matrix::Blas overrides invoke(), it has no knowledge of Matrix::PP.
COERCE could solve this. Granted, I could just define pp-to-blas(Matrix::PP $) returns Matrix::Blas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment