Anonymous user
Call an object method: Difference between revisions
→{{header|Perl 6}}: Clean-up and simplification
(→{{header|Rust}}: Added info about references to objects.) |
(→{{header|Perl 6}}: Clean-up and simplification) |
||
Line 1,028:
=={{header|Perl 6}}==
{{works with|Rakudo|2015.12}}
<lang perl6>class C { ▼
=== Basic method calls ===
method some-method(){ say 'I haz a method' }▼
multi method multi-example() { say 'No arguments given' }
multi method multi-example(Str $foo) { say 'String given' }
multi method multi-example(Int $foo) { say 'Integer given' }
};
# 'new' is actually a method, not a special keyword:
my $thing = Thing.new;
my $runtime-method-name = 'some-method';▼
# No arguments: parentheses are optional
$thing.regular-example;
$thing.regular-example();
};▼
$thing.multi-example;
$thing.multi-example();
# Arguments: parentheses or colon required
$thing.multi-example("This is a string");
$thing.multi-example: "This is a string";
$thing.multi-example(42);
$thing.multi-example: 42;
# Indirect (reverse order) method call syntax: colon required
my $foo = new Thing: ;
multi-example $thing: 42;
</lang>
=== Meta-operators ===
The <code>.</code> operator can be decorated with meta-operators.
<lang perl6>
my @array = <a z c d y>;
@array .= sort; # short for @array = @array.sort;
say @array».uc; # uppercase all the strings: A C D Y Z
</lang>
=== Anonymous methods ===
An anonymous method can be called by using the <code>&</code> sigil explicitly.
<lang perl6>
my $object = "a string"; # Everything is an object.
return "This is { self }.";
say $object.&example-method; # Outputs "This is a string."
</lang>
=={{header|PHP}}==
|