Thanks for all your help.  Interestingly enough the .perl method reports that 
the parameter passed to the new method is stored in the object whether it's 
defined as an attribute or not:

class MattTest {
    has Str $.bar ;
    submethod BUILD (:$foo) {  
        say "passed $foo";
        $.bar = 'def' ;
    }
    
}

my $test = MattTest.new(:foo('abc'));

# Check to see the class is the right kind of thing:

say $test.WHAT ;

# Output the class as a readable string

say $test.perl;

D:\Apps\Perl6>pugs ConstructorTest2.p6
passed abc
MattTest
\MattTest.new(("bar" => "def"), ("foo" => "abc"))

David Brunton <[EMAIL PROTECTED]> wrote: >I think so (after fixing a couple of 
minor typos).
>
>Does this mean that you can only pass defined attributes to a constructor ?  
>What if you want to pass parameters that are used during build but don't 
>actually need to be stored in the object ?

Hopefully you don't mind my cc'ing  the list- that way someone searching the 
archives doesn't get stung by my typos ;)

Creating an object should have been:

my MattTest $test .= new(:string<abc>); # TIMTOADY and all that ;)

You can pass objects to BUILD that you don't store in the object:

class MattTest {
    submethod BUILD  (:$foo) {
        say $foo;
    }
}

hth,
-db.

my $test = MattTest(:string('abc'));

# Check to see the class is the right kind of thing:

say $test.WHAT

# Output the class as a readable string

say $test.perl;




----- Original Message ----
From:  Matthew Keene 
To: [EMAIL PROTECTED]
Sent: Tuesday, May 15, 2007 5:02:56 AM
Subject: Error calling bless in non-default constructor

I'm trying to use a non-default constructor for a class under Pugs 6.2.13, like 
so

class MattTest {

  sub new (Class $class : Str $string) {
    say "Passed $string to the constructor for $class" ;
    return $class.bless ;
  }
  
}

my $test = MattTest.new('abc') ;

This is failing with the following output:

D:\Apps\Perl6>pugs ConstructorTest.p6
Passed abc to the constructor for MattTest
*** No such method in class MattTest: "&bless"
    at ConstructorTest.p6 line 5, column 12-25

Am I doing something wrong, or is bless currently unimplemented in Pugs, or is 
something else wrong.  I have more or less copied the code for the constructor 
from the test in oo/construction.t.

            
---------------------------------
How would you spend $50,000 to create a more sustainable environment in 
Australia?  Go to Yahoo!7 Answers and share your idea.




       
____________________________________________________________________________________Get
 the free Yahoo! toolbar and rest assured with the added security of spyware 
protection.
http://new.toolbar.yahoo.com/toolbar/features/norton/index.php

              

---------------------------------
Switch to Yahoo!7 Mail: Transfer all your contacts and emails from Hotmail and 
other providers to Yahoo!7 Mail. Switch now




       
---------------------------------
Get the free Yahoo! toolbar and rest assured with the added security of spyware 
protection.  

       
---------------------------------
How would you spend $50,000 to create a more sustainable environment in 
Australia?  Go to Yahoo!7 Answers and share your idea.

Reply via email to