How to resolve the algorithm Call an object method step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Call an object method step by step in the Raku programming language
Table of Contents
Problem Statement
In object-oriented programming a method is a function associated with a particular class or object. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class. Show how to call a static or class method, and an instance method of a class.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Call an object method step by step in the Raku programming language
Source code in the raku programming language
class Thing {
method regular-example() { 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;
# 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;
my @array = ;
@array .= sort; # short for @array = @array.sort;
say @array».uc; # uppercase all the strings: A C D Y Z
my $object = "a string"; # Everything is an object.
my method example-method {
return "This is { self }.";
}
say $object.&example-method; # Outputs "This is a string."
You may also check:How to resolve the algorithm Random Latin squares step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the D programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the GDScript programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the C++ programming language
You may also check:How to resolve the algorithm Intersecting number wheels step by step in the Visual Basic .NET programming language