daily pastebin goal
21%
SHARE
TWEET

Cannot look up attributes in a Point type object

a guest Dec 12th, 2017 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Point {
  2.     has int $.x;
  3.     has int $.y;
  4.  
  5.     method new( int $x, int $y ) {
  6.     return self.bless( :$x, :$y );
  7.     }
  8.  
  9.     method is-close( Point $other, Numeric $close = 5 ) returns Bool {
  10.     return ($.x - $other.x)**2 + ($.y - $other.y)**2 < $close;
  11.     }
  12. }
  13.  
  14. class Line {
  15.     has Point $.p1;
  16.     has Point $.p2;
  17.     has Bool $.is-short;
  18.  
  19.     method new( Point $p1, Point $p2 ) {
  20.     return self.bless( :$p1, :$p2 );
  21.     }
  22.  
  23.     submethod BUILD() {
  24.     $!is-short = $!p1.is-close( $!p2 );
  25.     }
  26. }
  27.  
  28. my $p1 = Point.new(1,2);
  29. my $p2 = Point.new(2,3);
  30. say $p1.is-close($p2);
  31.  
  32. my $line = Line.new( $p1, $p2 );
  33. say $line.is-short;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top