Skip to content

Instantly share code, notes, and snippets.

@phamer
Created October 19, 2015 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phamer/71b8b38e565bca363b81 to your computer and use it in GitHub Desktop.
Save phamer/71b8b38e565bca363b81 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
use v6;
subset NonNegRat of Rat where * >= 0;
class Point {
has $.x is rw is required;
has $.y is rw is required;
}
class Dimension {
has NonNegRat $.x is rw is required;
has NonNegRat $.y is rw is required;
}
class Room {
has Dimension $.dim;
method new( $x, $y ) {
# what to do here?
# I want to create a new Room instance, with $.dim constructed with $x, $y as dimensions
}
method info() {
say "Hi, I'm a room of dimensions ", $.dim.x, " x ", $.dim.y;
}
}
# ignore $roomfile for now
sub MAIN( $roomfile )
{
# w/o method new() declaration, this works as intended
#my $room = Room.new( dim => Dimension.new( x => 400.0, y => 500.0 ) );
#$room.info();
my $room2 = Room.new( 4.0, 5.0 );
$room2.info();
}
@jonathanstowe
Copy link

Try :

#!/usr/bin/env perl6

use v6;

subset NonNegRat of Rat where * >= 0;

class Point {
  has $.x is rw is required;
  has $.y is rw is required;
}

class Dimension {
  has NonNegRat $.x is rw is required;
  has NonNegRat $.y is rw is required;
}

class Room {
  has Dimension $.dim;

  submethod BUILD( :$x, :$y ) {
      $!dim = Dimension.new(:$x, :$y);
  }

  method info() {
      say "Hi, I'm a room of dimensions ", $.dim.x, " x ", $.dim.y;
  }
}

# ignore $roomfile for now
sub MAIN( $roomfile )
{
  # w/o method new() declaration, this works as intended
  #my $room = Room.new( dim => Dimension.new( x => 400.0, y => 500.0 ) );
  #$room.info();

  my $room2 = Room.new( :x(4.0), :y(5.0) );
  $room2.info();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment