String Class, odd behavior when calling Outout.printString

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

String Class, odd behavior when calling Outout.printString

throwinshapes
This post was updated on .
In attempting to debug my string class setInt() method, I discovered a more core issue - that when I attempt to print the string, I get a printed string equal to the maximum length, rather than the current length. And on top of that, appendChar() is always 3 elements in ("A" and "B" below), rather than the first.



I have been trying to diagnose this issue for a while now, it's driving me crazy, as the implementation of new(), appendChar(), and charAt() seem pretty straightforward. I've attached the code. I appreciate any help!

Main.jack  StringDev.jack
Reply | Threaded
Open this post in threaded view
|

Re: String Class, odd behavior when calling Outout.printString

dolomiti7
This post was updated on .
I didn't check whether your code in StringDev is correct or not. But what you are trying to do in order to debug, is for sure not correct. Output.printString() expects a parameter of the type String, not StringDev. Though the 2 classes may be identical in terms of the API, they are of different type. A Java Compiler for example would not accept this (unless both classes are derived from a common interface and the printString method expects this interface as a parameter).

So what happens in this case is that Output.printString gets passed a reference to the object and expects it to be a String.
Depending on the implementation, printString will typically call String.charAt (not StringDev.charAt!!!) and pass on the reference to your StringDev as object reference. The String.charAt is relying on the internal field structure and implementation details of String. So unless your StringDev class is exactly identical to String (I.e. the order and semantics of the fields), it will not work. And even if it does, it is purely a coincidence and principally a programming error. Obviously the Compiler does not perform a type checking in this case (which is beyond the scope of n2t), otherwise it would have prevented this attempt.

So the most straightforward way is to rename your class, class references and  file back to String and test it. Otherwise a more complicated way would be to add a toString method to your class that creates a new String and copies your string char by char into it, then in your test routine, you first convert to a string and then call printString. But that approach is not only more complicated, but also comes with the risk of introducing bugs in the conversion.
Reply | Threaded
Open this post in threaded view
|

Re: String Class, odd behavior when calling Outout.printString

throwinshapes
Thank you! Great detailed explanation, that makes sense. I was close but didn't fully understand it - I actually saw values in the heap at breakpoints appear correct, but in a different order between the two classes.

What this means for my process: is I need to start developing these classes under their final name using the given skeletal class file from the start. We want to override the built-in right at the start of development. i.e., we are saying: "compile String in this way, so that when a given method is called, we need to know exactly what the internal structure looks like of the fields, etc. It is not enough that the names/API is the same".

It really gives meaning to the fact that Jack is not a typed language.