This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,8 @@
TUPLE: my-class foo bar baz ;
M: my-class quux foo>> 20 + ;
C: <my-class> my-class
10 20 30 <my-class> quux ! result: 30
TUPLE: my-child-class < my-class quxx ;
C: <my-child-class> my-child-class
M: my-child-class foobar 20 >>quux ;
20 20 30 <my-child-class> foobar quux ! result: 30

View file

@ -0,0 +1,6 @@
class class_name[ ( param_list ) ] [ from inh1[, inh2, ..., inhN] ]
[ static block ]
[ properties declaration ]
[init block]
[method list]
end

View file

@ -0,0 +1,15 @@
class mailbox( max_msg )
capacity = max_msg * 10
name = nil
messages = []
init
printl( "Box now ready for ", self.capacity, " messages." )
end
function slot_left()
return self.capacity - len( self.messages )
end
end

View file

@ -0,0 +1,2 @@
m = mailbox( 10 )
// Ouputs: Box now ready for 100 messages.

View file

@ -0,0 +1,26 @@
class MyClass {
read_slot: 'instance_var # creates getter method for @instance_var
@@class_var = []
def initialize {
# 'initialize' is the constructor method invoked during 'MyClass.new' by convention
@instance_var = 0
}
def some_method {
@instance_var = 1
@another_instance_var = "foo"
}
# define class methods: define a singleton method on the class object
def self class_method {
# ...
}
# you can also name the class object itself
def MyClass class_method {
# ...
}
}
myclass = MyClass new

View file

@ -0,0 +1,27 @@
class MyClass
{
// an instance variable
Int x
// a constructor, providing default value for instance variable
new make (Int x := 1)
{
this.x = x
}
// a method, return double the number x
public Int double ()
{
return 2 * x
}
}
class Main
{
public static Void main ()
{
a := MyClass (2) // instantiates the class, with x = 2
b := MyClass() // instantiates the class, x defaults to 1
c := MyClass { x = 3 } // instantiates the class, sets x to 3
}
}

View file

@ -0,0 +1,15 @@
/** Ye olde classe declaration */
class Stuff {
/** Heare bee anne instance variable declared */
def guts
/** This constuctor converts bits into Stuff */
Stuff(injectedGuts) {
guts = injectedGuts
}
/** Brethren and sistren, let us flangulate with this fine flangulating method */
def flangulate() {
println "This stuff is flangulating its guts: ${guts}"
}
}

View file

@ -0,0 +1,16 @@
def stuff = new Stuff('''
I have made mistakes in the past.
I have made mistakes in the future.
-- Vice President Dan Quayle
''')
stuff.flangulate()
stuff.guts = '''
Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country and our people,
and neither do we.
-- President George W. Bush
'''
stuff.flangulate()

View file

@ -0,0 +1,11 @@
coclass 'exampleClass'
exampleMethod=: monad define
1+exampleInstanceVariable
)
create=: monad define
'this is the constructor'
)
exampleInstanceVariable=: 0

View file

@ -0,0 +1 @@
exampleObject=: conew 'exampleClass'

View file

@ -0,0 +1,24 @@
Section Header
+ name := SAMPLE;
Section Inherit
- parent : OBJECT := OBJECT;
Section Private
+ variable : INTEGER <- 0;
Section Public
- some_method <- (
variable := 1;
);
- main <- (
+ sample : SAMPLE;
sample := SAMPLE.clone;
sample.some_method;
);

View file

@ -0,0 +1,20 @@
:- object(metaclass,
instantiates(metaclass)).
:- public(new/2).
new(Instance, Value) :-
self(Class),
create_object(Instance, [instantiates(Class)], [], [state(Value)]).
:- end_object.
:- object(class,
instantiates(metaclass)).
:- public(method/1).
method(Value) :-
::state(Value).
:- private(state/1).
:- end_object.

View file

@ -0,0 +1,7 @@
| ?- class::new(Instance, 1).
Instance = o1
yes
| ?- o1::method(Value).
Value = 1
yes