1. my class Duration is Cool does Real {
  2. has Rat $.tai = 0;
  3. # A linear count of seconds.
  4. method new($tai) { self.bless: tai => $tai.Rat }
  5. method Bridge(Duration:D:) { $!tai.Num }
  6. method Num (Duration:D:) { $!tai.Num }
  7. method Rat (Duration:D:) { $!tai }
  8. method narrow(Duration:D:) { $!tai.narrow }
  9. multi method Str(Duration:D:) { ~$.tai }
  10. multi method perl(Duration:D:) { "Duration.new({$.tai.perl})" }
  11. }
  12. multi sub prefix:<->(Duration:D $a) {
  13. Duration.new: -$a.tai;
  14. }
  15. multi sub infix:<+>(Duration:D $a, Real $b) {
  16. Duration.new: $a.tai + $b;
  17. }
  18. multi sub infix:<+>(Real $a, Duration:D $b) {
  19. Duration.new: $a + $b.tai;
  20. }
  21. multi sub infix:<+>(Duration:D $a, Duration:D $b) {
  22. Duration.new: $a.tai + $b.tai;
  23. }
  24. multi sub infix:<->(Duration:D $a, Real $b) {
  25. Duration.new: $a.tai - $b;
  26. }
  27. multi sub infix:<->(Duration:D $a, Duration:D $b) {
  28. Duration.new: $a.tai - $b.tai;
  29. }
  30. multi sub infix:<%>(Duration:D $a, Real $b) {
  31. Duration.new: $a.tai % $b
  32. }