Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Constructor (object-oriented programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Raku === In [[Raku (programming language)|Raku]], even more boilerplate can be omitted, given that a default ''new'' method is inherited, attributes can be specified, and whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a ''BUILD'' method which will get called to allow for custom initialization. A ''TWEAK'' method can be specified to post-process any attributes already (implicitly) initialized. <syntaxhighlight lang="perl6"> class Person { has Str $.first-name is required; # First name (a string) can only be set at # construction time (the . means "public"). has Str $.last-name is required; # Last name (a string) can only be set at # construction time (a ! would mean "private"). has Int $.age is rw; # Age (an integer) can be modified after # construction ('rw'), and is not required # during the object instantiation. # Create a 'full-name' method which returns the person's full name. # This method can be accessed outside the class. method full-name { $!first-name.tc ~ " " ~ $!last-name.tc } # Create a 'has-age' method which returns true if age has been set. # This method is used only inside the class so it's declared as "private" # by prepending its name with a ! method !has-age { self.age.defined } # Check custom requirements method TWEAK { if self!has-age && $!age < 18 { # No under 18 die "No person under 18"; } } } </syntaxhighlight> The Person class is instantiated like this: <syntaxhighlight lang="perl6"> my $p0 = Person.new( first-name => 'Sam', last-name => 'Ashe', age => 42 ); my $p1 = Person.new( first-name => 'grace', last-name => 'hopper' ); say $p1.full-name(); # OUTPUT: «Grace Hopper» </syntaxhighlight> Alternatively, the [[named parameter]]s can be specified using the colon-pair syntax in Perl 6: <syntaxhighlight lang="perl6"> my $p0 = Person.new( :first-name<Sam>, :last-name<Ashe>, :age(42) ); my $p1 = Person.new( :first-name<Grace>, :last-name<Hopper> ); </syntaxhighlight> And should you have set up variables with names identical to the named parameters, you can use a shortcut that will use the '''name''' of the variable for the named parameter: <syntaxhighlight lang="perl6"> my $first-name = "Sam"; my $last-name = "Ashe"; my $age = 42; my $p0 = Person.new( :$first-name, :$last-name, :$age ); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)