A couple of months ago I came across Musings of an OS plumber. The one posting that really caught my attention was Using Types To Create Object Orientated Korn Shell 93 Scripts.
Ever since then I have been searching the Web looking for more information/examples of them. In all that time I've come to the conclusion that they are very rare because I have only found 3 examples and 3 documents (one of which is 'Type Variables' in the ksh93 man page).
If anybody here knows where I can find more on this feature then please can you pass it on to me. My plan is to write an article on this and post it here. I know of at least one person that would find it helpful.
The
kshlanguage is pretty neat and I'm glad somebody is taking an interest in the underused features. (Language?! Quite so, it is a fully functional and fairly powerful language that runs slow as... but that's another story.)I'll give you this example and I'll fill in an explanation about what I am doing after.
When run with ksh2020 (which is better than I remembered) this resulted in:
I think it is explained in the man page that
Name_T _means inheritance. Class names start with caps and end with_T. You don't have to but it's a good convention. Variables that are prefixed by_.are class variables.By way of explanation,
I created the
TheTime_Tclass to give the idea of a get function. "At the office," we have anowbuilt-in that gives Unix seconds since 1970 but for show and tell, I created this function that counts up each time it is called so we give the idea that time is progressing.You can see the three classes
Upper_T,Middle_TandLower_T, each is based upon the one above, inheriting functions (aka, methods) and variables (properties). I'm not sure if it is in the man pages but_is the current object so declaring_to be of typeUpper_Tsays thatMiddle_Tis derived fromUpper_T.The
Upper_T.initializefunction has two count variables, one is declared with the-S, static. As always, static means shared between instances so thecountSgets incremented for all instancescountIonly gets incremented for each instance. Why iscountIincremented differently? I'll leave that as an exercise for the student (I don't know, that's what works).A bit more about the syntax,
You can see
_.two="midEnd". (I did not need the quotes but they don't hurt.) That sets the two variable in thatMiddle_Tinstance.Observe the
.sh.type.Upper_T.initialize ${!_}This is calling the initialize function in that class. There's should be a way to do it with something like_._.initializebut it does not work.We see the echo of
init of Upper: ${!_}. When this was run as a part of a variable of typeUpper_T, this printed the variable name. When run when called directly from a lower class, it displayed the class name.I put in a bunch of goofy variable manipulations that don't serve any purpose and are not particularly illustrative but maybe they will help getting the idea.
My colleague loved ksh to distraction and wrote massive programs with great complicated classes. I wish he had loved Python that much so the code would be written in a language that is actually controlled and generally agreed upon rather than just what happens to work.
Questions?