Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit d27a98b

Browse files
committed
Added example of object-method dumping
1 parent 500674c commit d27a98b

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

examples/object_methods.mon

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env monkey
2+
//
3+
// Object-methods are defined against types in monkey.
4+
//
5+
// For example if you defined the function string.blah():
6+
//
7+
// function string.blah() { puts( "OK\n"); };
8+
//
9+
// You could then invoke it via:
10+
//
11+
// "input".blah();
12+
//
13+
// (The value it was invoked against would be available via the
14+
// variable "this", if you wished to use it.)
15+
//
16+
// This script dumps all available object-methods which are available
17+
// for each type.
18+
//
19+
20+
21+
//
22+
// Create an array holding various types
23+
//
24+
let t = [ [], 3.13, fn(){} , {} , 3, "steve" ];
25+
26+
//
27+
// Walk over the types
28+
//
29+
let i = 0;
30+
for( i < len(t) ) {
31+
32+
//
33+
// Show the type + methods.
34+
//
35+
let item = t[i];
36+
let metod = item.methods();
37+
puts( "Object type ", type(item), "\n");
38+
39+
//
40+
// Show the methods.
41+
//
42+
let j = 0;
43+
for ( j < len( metod ) ) {
44+
puts( "\t", metod[j], "\n");
45+
j++;
46+
}
47+
i++;
48+
}

0 commit comments

Comments
 (0)