Call an object method: Difference between revisions
Content added Content deleted
(→{{header|Rust}}: Added info about references to objects.) |
(→{{header|Perl 6}}: Clean-up and simplification) |
||
Line 1,028: | Line 1,028: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{works with|Rakudo|2015.12}} |
|||
⚫ | |||
=== Basic method calls === |
|||
⚫ | |||
⚫ | |||
⚫ | |||
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' } |
|||
}; |
}; |
||
my C $a-c.=new; # we need an instance of C |
|||
$a-c.some-method; # so we can call a method |
|||
# 'new' is actually a method, not a special keyword: |
|||
sub not-a-method(Any:D $obj){ say $obj.WHAT }; # *.WHAT stringifies to the typename in parentheses |
|||
my $thing = Thing.new; |
|||
$a-c.¬-a-method; # output: '(C)' |
|||
my @many-cs = C.new xx 3; # a List of 3 Cs |
|||
@many-cs>>.¬-a-method; # let's call not-a-method on all 3 Cs at once |
|||
# the >>. hyperoperator is a candidate for autothreading so your order of execution may vary |
|||
⚫ | |||
$a-c."$runtime-method-name"(); # here some very late binding |
|||
# No arguments: parentheses are optional |
|||
my multi method free-floating-method($self:){ # my is required or the compiler thinks we misplaced a method |
|||
$thing.regular-example; |
|||
say 'i haz a C' if $self ~~ C # we do the type check by hand |
|||
$thing.regular-example(); |
|||
⚫ | |||
$thing.multi-example; |
|||
$thing.multi-example(); |
|||
# Arguments: parentheses or colon required |
|||
free-floating-method($a-c); # $self is bound to the first parameter |
|||
$thing.multi-example("This is a string"); |
|||
$a-c.&free-floating-method; # dito but automatically |
|||
$thing.multi-example: "This is a string"; |
|||
$thing.multi-example(42); |
|||
$thing.multi-example: 42; |
|||
# Indirect (reverse order) method call syntax: colon required |
|||
$a-c.?does-not-exist; # this method does not exist so it's not called thanks to .? |
|||
my $foo = new Thing: ; |
|||
multi-example $thing: 42; |
|||
</lang> |
|||
=== Meta-operators === |
|||
use MONKEY-TYPING; |
|||
augment class Int { |
|||
method does-not-exists(){} # This is one way to add a method. As usual there are more then one. |
|||
} |
|||
The <code>.</code> operator can be decorated with meta-operators. |
|||
$a-c.?does-not-exists; # now it exists and we can call it |
|||
<lang perl6> |
|||
my multi method free-floating-method(C:D $self:){} # we could let the compiler do the type check |
|||
my @array = <a z c d y>; |
|||
my multi method free-floating-method(Int:D $self:){} # or let it pick the right candidate |
|||
@array .= sort; # short for @array = @array.sort; |
|||
my @a-good-mix = (Int.new((1..100).roll),C.new).roll xx 5; # let's have a mixture of Cs and Ints |
|||
@a-good-mix>>.&free-floating-method; # and let Perl 6 pick the right candidate for us |
|||
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." |
|||
C.some-method(); # actually we don't really need an instance of C. We can call class methods aswell.</lang> |
|||
</lang> |
|||
Note that, unlike in Perl 5, class names are type objects, not strings. (Type objects are typed variants of the undefined value in Perl 6.) |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |