|
|
|
The Other Reference TypesThis section about "other reference types" covers these types:
Just like the container types, these types are reference types: When a data item of a basic type is stored in a variable, it is not the data item itself that is stored, but a reference to it. The Data Type programThe data type program in Pike is used to contain class definitions. Not the source code, i. e. the program text, but the internal representation that Pike generates when it has read the source code. As described above in the chapter about object-oriented programming, programs and classes are the same in Pike. Both an explicit class definition (that is, with the keyword class) and a source code file can be used to define a program/class. Here are some useful things that you can do with programs:
Read more about classes and objects in the chapter about object-oriented programming. The Data Type objectIn object-oriented programming, the data items that are instances (also called clones) of a class are called objects. The data type object is used to store such objects. An object is an instance of any class. Usually you use the more specific data type object(classname), which means an object that is an instance of the class classname. You can also write just classname instead of object(classname), and this is the recommended form. Here are some useful things that you can do with objects:
Read more about classes and objects in the chapter about object-oriented programming. The Data Type functionSomething else that may surprise you is that there is a data type for methods. Sometimes you want to refer to "any method". Take for example the built-in method map, which is used to apply an operation to all the elements in an array. You call map with (at least) two arguments: the array to go through, and the method to call for each element: void write_one(int x) { write("Number: " + x + "\n"); } int main() { array(int) all_of_them = ({ 1, 5, -1, 17, 17 }); map(all_of_them, write_one); return 0; } The first argument to map is an array, and the second argument is of the type function. As you can see, you get a method reference of the type function by just typing the method name, without the parentheses used in method calls. A method stored in a variable can be called just like a normal method: function w = write; w("Hello!\n"); Here are some useful things that you can do with method references:
|
|||||||||