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