2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1 @@
instance message:param1:param2.

View file

@ -0,0 +1 @@
instance message &subj1:param1 &subj2:param2.

View file

@ -0,0 +1,27 @@
defmodule ObjectCall do
def new() do
spawn_link(fn -> loop end)
end
defp loop do
receive do
{:concat, {caller, [str1, str2]}} ->
result = str1 <> str2
send caller, {:ok, result}
loop
end
end
def concat(obj, str1, str2) do
send obj, {:concat, {self(), [str1, str2]}}
receive do
{:ok, result} ->
result
end
end
end
obj = ObjectCall.new()
IO.puts(obj |> ObjectCall.concat("Hello ", "World!"))

View file

@ -0,0 +1,26 @@
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;

View file

@ -0,0 +1,4 @@
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

View file

@ -0,0 +1,6 @@
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."

View file

@ -1,36 +0,0 @@
class C {
method some-method(){ say 'I haz a method' }
};
my C $a-c.=new; # we need an instance of C
$a-c.some-method; # so we can call a method
sub not-a-method(Any:D $obj){ say $obj.WHAT }; # *.WHAT stringifies to the typename in parentheses
$a-c.&not-a-method; # output: '(C)'
my @many-cs = C.new xx 3; # a List of 3 Cs
@many-cs>>.&not-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
my $runtime-method-name = 'some-method';
$a-c."$runtime-method-name"(); # here some very late binding
my multi method free-floating-method($self:){ # my is required or the compiler thinks we misplaced a method
say 'i haz a C' if $self ~~ C # we do the type check by hand
};
free-floating-method($a-c); # $self is bound to the first parameter
$a-c.&free-floating-method; # dito but automatically
$a-c.?does-not-exist; # this method does not exist so it's not called thanks to .?
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.
}
$a-c.?does-not-exists; # now it exists and we can call it
my multi method free-floating-method(C:D $self:){} # we could let the compiler do the type check
my multi method free-floating-method(Int:D $self:){} # or let it pick the right candidate
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
C.some-method(); # actually we don't really need an instance of C. We can call class methods aswell.

View file

@ -0,0 +1,21 @@
// define a rudimentary class
class HelloWorld
{
public static void sayHello()
{
println("Hello, world!");
}
public void sayGoodbye()
{
println("Goodbye, cruel world!");
}
}
// call the class method
HelloWorld.sayHello();
// create an instance of the class
HelloWorld hello = new HelloWorld();
// and call the instance method
hello.sayGoodbye();

View file

@ -23,4 +23,9 @@ fn main() {
// get the answer to life
// by calling the instance method of object foo
println!("The answer to life is {}.", foo.get_the_answer_to_life());
// Note that in Rust, methods still work on references to the object.
// Rust will automatically do the appropriate dereferencing to get the method to work:
let lots_of_references = &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&foo;
println!("The answer to life is still {}." lots_of_references.get_the_answer_to_life());
}