Example: User-defined generics
- Last Updated: May 6, 2024
- 3 minute read
- OpenEdge
- Version 13.0
- Documentation
This example is a demo that uses user-defined generics.
- container.cls—A generic class
- cobject.cls—An abstract class
- person.cls—A class that inherits from cobject.cls
- place.cls—A class that inherits from cobject.cls
- thing.cls— A class that inherits from cobject.cls
- demo.p—A procedure that runs the demo
First, examine the cobject class which is an abstract class:
| cobject.cls |
|---|
|
Note that the cobject class has an abstract method called
doSomething().
Next, examine the three classes (person, place, and
thing). These classes inherit from cobject and
override the doSomething() method:
| person.cls |
|---|
|
| place.cls |
|---|
|
| thing.cls |
|---|
|
The doSomething() method simply returns what they are (I am
person/place/thing).
Next, look at the container class:
| container.cls |
|---|
|
In the container class, V is the type parameter and
cobject is the type constraint. The constructor uses dynamic
programming to create an instance of the type that is passed (person, place, or thing).
The class also has a method, which calls the appropriate doSomething()
method depending on the type.
Next, look at demo.p. Note that there are two parts: Demo 1 and Demo2. They both do the same thing but Demo 2 defines the variables dynamically at runtime.
| demo.p |
|---|
|
In Demo 1 (non-dynamic version), three variables are defined: cpersonRef,
cplaceRef, and cthingRef and then an instance of
each is created. A message statement follows the creation of each
instance which calls the doSomething() method for that container
type.
In Demo 2 (dynamic version), cref is defined as a
Progress.Lang.Object. Nouns is a character list of
"person,place,thing". Within the loop, dynamic-new
is called on an expression which evaluates to either
container<person>, container<place>,
or container<thing> and assigns it to cref.
Then, dynamic-invoke is called on the doSomthing
method of cref.
After running demo.p, you see the following output:
| demo.out |
|---|
|
The first three lines are from the non-dynamic version and the next three are from the dynamic version. As you can see, they produce the same output.