1. my class Bag does Baggy {
  2. has Int $!total;
  3. has $!WHICH;
  4. #--- introspection methods
  5. multi method WHICH(Bag:D:) {
  6. nqp::if(
  7. nqp::attrinited(self,Bag,'$!WHICH'),
  8. $!WHICH,
  9. $!WHICH := self!WHICH
  10. )
  11. }
  12. method total(Bag:D: --> Int:D) {
  13. nqp::if(
  14. nqp::attrinited(self,Bag,'$!total'),
  15. $!total,
  16. $!total := self!TOTAL
  17. )
  18. }
  19. #--- interface methods
  20. multi method DELETE-KEY(Bag:D: \k) {
  21. X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
  22. }
  23. #--- selection methods
  24. multi method grab(Bag:D: $count?) {
  25. X::Immutable.new( method => 'grab', typename => self.^name ).throw;
  26. }
  27. multi method grabpairs(Bag:D: $count?) {
  28. X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
  29. }
  30. #--- coercion methods
  31. method Bag { self }
  32. method BagHash { BagHash.new-from-pairs(%!elems.values) }
  33. method Mix {
  34. nqp::p6bindattrinvres(nqp::create(Mix),Mix,'%!elems',%!elems)
  35. }
  36. method MixHash { MixHash.new-from-pairs(%!elems.values) }
  37. proto method classify-list(|) {
  38. X::Immutable.new(:method<classify-list>, :typename(self.^name)).throw;
  39. }
  40. proto method categorize-list(|) {
  41. X::Immutable.new(:method<categorize-list>, :typename(self.^name)).throw;
  42. }
  43. }