1. my class Mix does Mixy {
  2. has $!WHICH;
  3. has Real $!total;
  4. #--- interface methods
  5. method SET-SELF(Mix:D: \elems) {
  6. nqp::if(
  7. nqp::elems(elems),
  8. nqp::stmts( # need to have allocated %!elems
  9. nqp::bindattr(%!elems,Map,'$!storage',elems),
  10. self
  11. ),
  12. mix()
  13. )
  14. }
  15. multi method DELETE-KEY(Mix:D: \k) {
  16. X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
  17. }
  18. #--- introspection methods
  19. multi method WHICH(Mix:D:) {
  20. nqp::if(
  21. nqp::attrinited(self,Mix,'$!WHICH'),
  22. $!WHICH,
  23. $!WHICH := self!WHICH
  24. )
  25. }
  26. method total(Mix:D: --> Real:D) {
  27. nqp::if(
  28. nqp::attrinited(self,Mix,'$!total'),
  29. $!total,
  30. $!total := Rakudo::QuantHash.MIX-TOTAL(self.raw_hash)
  31. )
  32. }
  33. #--- object creation methods
  34. multi method new(Mix:_:) {
  35. nqp::if(
  36. nqp::eqaddr(self.WHAT,Mix),
  37. mix(),
  38. nqp::create(self)
  39. )
  40. }
  41. #--- selection methods
  42. multi method grab($count? --> Real:D) {
  43. X::Immutable.new( method => 'grab', typename => self.^name ).throw;
  44. }
  45. multi method grabpairs($count? --> Real:D) {
  46. X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
  47. }
  48. #--- coercion methods
  49. multi method Mix(Mix:D:) { self }
  50. method clone() { nqp::clone(self) }
  51. proto method classify-list(|) {
  52. X::Immutable.new(:method<classify-list>, :typename(self.^name)).throw;
  53. }
  54. proto method categorize-list(|) {
  55. X::Immutable.new(:method<categorize-list>, :typename(self.^name)).throw;
  56. }
  57. }