Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,8 @@
package My_Class is
type Object is tagged private;
procedure Primitive(Self: Object); -- primitive subprogram
procedure Dynamic(Self: Object'Class);
procedure Static;
private
type Object is tagged null record;
end My_Class;

View file

@ -0,0 +1,18 @@
package body My_Class is
procedure Primitive(Self: Object) is
begin
Put_Line("Hello World!");
end Primitive;
procedure Dynamic(Self: Object'Class) is
begin
Put("Hi there! ... ");
Self.Primitive; -- dispatching call: calls different subprograms,
-- depending on the type of Self
end Dynamic;
procedure Static is
begin
Put_Line("Greetings");
end Static;
end My_Class;

View file

@ -0,0 +1,11 @@
package Other_Class is
type Object is new My_Class.Object with null record;
overriding procedure Primitive(Self: Object);
end Other_Class;
package body Other_Class is
procedure Primitive(Self: Object) is
begin
Put_Line("Hello Universe!");
end Primitive;
end Other_Class;

View file

@ -0,0 +1,20 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Call_Method is
package My_Class is ... -- see above
package body My_Class is ... -- see above
package Other_Class is ... -- see above
package body Other_Class is ... -- see above
Ob1: My_Class.Object; -- our "root" type
Ob2: Other_Class.Object; -- a type derived from the "root" type
begin
My_Class.Static;
Ob1.Primitive;
Ob2.Primitive;
Ob1.Dynamic;
Ob2.Dynamic;
end Call_Method;

View file

@ -0,0 +1,29 @@
include FMS-SI.f
:class animal
variable cnt 0 cnt ! \ static instance variable
:m init: 1 cnt +! ;m
:m cnt: cnt @ . ;m
;class
:class cat <super animal
:m speak ." meow" ;m
;class
:class dog <super animal
:m speak ." woof" ;m
;class
cat Frisky \ instantiate a cat object named Frisky
dog Sparky \ instantiate a dog object named Sparky
\ The class method cnt: will return the number of animals instantiated
\ regardless of which animal object is used.
\ The instance method speak will respond differently depending
\ on the class of the instance object.
Frisky cnt: \ => 2 ok
Sparky cnt: \ => 2 ok
Frisky speak \ => meow ok
Sparky speak \ => woof ok

View file

@ -0,0 +1,14 @@
! type declaration
type my_type
contains
procedure, pass :: method1
procedure, pass, pointer :: method2
end type my_type
! declare object of type my_type
type(my_type) :: mytype_object
!static call
call mytype_object%method1() ! call method1 defined as subroutine
!instance?
mytype_object%method2() ! call method2 defined as function

View file

@ -0,0 +1 @@
x.y()

View file

@ -0,0 +1,10 @@
% avoid infinite metaclass regression by
% making the metaclass an instance of itself
:- object(metaclass,
instantiates(metaclass)).
:- public(me/1).
me(Me) :-
self(Me).
:- end_object.

View file

@ -0,0 +1,9 @@
:- object(class,
instantiates(metaclass)).
:- public(my_class/1).
my_class(Class) :-
self(Self),
instantiates_class(Self, Class).
:- end_object.

View file

@ -0,0 +1,4 @@
:- object(instance,
instantiates(class)).
:- end_object.

View file

@ -0,0 +1,7 @@
| ?- class::me(Me).
Me = class
yes
| ?- instance::my_class(Class).
Class = class
yes

View file

@ -1,2 +1,36 @@
ClassName.methodname(); # call class method
$instance.methodname(); # call instance method
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,3 @@
$Date = Get-Date
$Date.AddDays( 1 )
[System.Math]::Sqrt( 2 )