I am trying to simulate a kind of pointer used in another obscure programming paradigm, so I can port some code to Java. The other language is not object-oriented, and was loosely inspired by Pascal.
In the original language, we can write code like this. First, working with text.
// Start with text.
Text myVar = "Bonjour"
Pointer myPointer = ->myVar // Referencing a string variable, storing the reference in another variable of type `Pointer`.
Message( myPointer-> ) // Dereferencing the pointer, to retrieve `myVar`, and pass the string to a command `Display` that displays the message on screen in a dialog box.
Then, switching to numbers.
// Switch gears, to work with an number.
Integer vResult = ( Random % ( vEnd - vStart + 1 ) ) + vStart // Generate random number.
myPointer = ->vResult // The same pointer now points to numeric variable rather than a textual variable.
We can assign a pointer by the text of a variable name.
myPointer = Get pointer( "var" + String($i) ) // Generate pointer variable named `var1`, or `var2`, etc.
We can ask the pointer for a code number representing the data type of the value to which it is pointing (the data type of the referent).
typeCodeNumber = Type( myPointer ) // Returns 11 for an integer, 22 for text.
In this other language, the compiler does provide for type-safety. But when using pointers in this fashion, we sacrifice type-safety. The compiler emits a warning that the code usage is ambiguous with regard to type.
My idea to port this code is to define a XPointer class as well as classes for the types such as XText and XInteger.
I need to hold a reference to an object of any of a dozen specific known types, including to another pointer. I can hard-code the dozen types, no need to be open to all types.
These dozen types do not share an interface nor abstract class other than Object. And even if they did share an interface/superclass, I do not want them returned as a superclass but as their original concrete class. As they entered the pointer, so should they emerge from the pointer.
My current plan is to define a XPointer class in Java with a pair of reference and dereference methods:
XPointer::ref( x )where you pass an object ofDog,Truck, orSculptureclass, or even anotherXPointerobject.XPointer::deref ⇒ xwhere x is an object recognized as its original type, aDog, aTruck, or aSculptureor even anotherXPointerobject, rather than a mereObjectobject.
➥ Is there some way to do this Java? Perhaps with Generics?
➥ If not possible in Java, I could reluctantly switch to Kotlin. Can this pointer functionality can be done in Kotlin running on a JVM?
So code my look like this:
XPointer p = new XPointer() ; // Points to nothing, null.
p.ref( new Dog() ) ; // Pointer points to a `Dog` object.
p.deref().bark() ; // Pointer can retrieve the `Dog` as such, a `Dog` object.
p.ref( someTruck ) ; // The pointer can switch to pointing to an object of an entirely different type. The `Dog` object has been replaced by a `Truck` object.
p.deref().honk() ; // Dereference the stored `Truck` object as such.
And a pointer to a pointer.
XPointer p2 = new XPointer() ; // Points to nothing, null.
p2.ref( p ) ; // 2nd pointer points to a pointer that points to a `Truck` object.
p2.deref().deref().honk() ; // Dereference the stored `Truck` object as such.
If there is a better route towards such a pointer-simulation, I am open to suggestions. Elegance is not required; any hack will do.