tags:

views:

353

answers:

2

I recently started using the module MooseX::Declare. I love it for its syntax. It's elegant and neat. Has anyone come across cases where you would want to write many functions (some of them big) inside a class and the class definition running into pages? Is there any workaround to make the class definition to just have the functions declared and the real function definition outside the class?

What I am look for is something like this -

class BankAccount {
    has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );
    # Functions Declaration.
    method deposit(Num $amount);
    method withdraw(Num $amount);
}

# Function Definition.
method BankAccount::deposit (Num $amount) {
    $self->balance( $self->balance + $amount );
}

method BankAccount::withdraw (Num $amount) {
    my $current_balance = $self->balance();
    ( $current_balance >= $amount )
    || confess "Account overdrawn";
    $self->balance( $current_balance - $amount );
}

I can see that there is a way to make the class mutable. Does anyone know how to do it?

+7  A: 

Easy (but needs adding to the doc).

class BankAccount is mutable {
}

As an aside, why are you defining your methods outside the class?

You can just go

class BankAccount is mutable {
    method foo (Int $bar) {
         # do stuff
    }
}
Penfold
A: 

I want my class definition to be short, and to give an abstract idea of what the class is for. I like the way its been done in C++ where you have an option to define functions inline or outside the class using the scope resolution operator. This makes the class definition short and neat. This is what I am looking for.

Thanks for your time.

sachinjsk
Send an email to the author, thank him for his excellent work, tell him that you love the syntax, and then ask him if he thinks such a thing is possible, and if he would be willing to do it. The worst that can happen is he can say 'no'.Another place to post would be the moose mailing list.
daotoad