What is wrong with the invocation "Output.printString(s.setInt(i))" ?

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

What is wrong with the invocation "Output.printString(s.setInt(i))" ?

Rather Iffy
This code gives an error when run on VMEmulator:  

   /** Prints i starting at the cursor location, and advances the
     *  cursor appropriately. */
    function void printInt(int i) {
        var String s ;                                                          
        let  s = String.new(8);                                                
        do Output.printString(s.setInt(i));                                    
        return;                                                                
    }



When i changed the code to :

    /** Prints i starting at the cursor location, and advances the
     *  cursor appropriately. */
    function void printInt(int i) {
        var String s ;                                                          
        let  s = String.new(8);                                                
        do s.setInt(i);                                                        
        do Output.printString(s);                                              
        return;                                                                
    }

it runs perfectly.



What is wrong with the invocation "Output.printString(s.setInt(i))" ?

--Chrit
Reply | Threaded
Open this post in threaded view
|

Re: What is wrong with the invocation "Output.printString(s.setInt(i))" ?

cadet1620
Administrator
Rather Iffy wrote
What is wrong with the invocation "Output.printString(s.setInt(i))" ?
String.setInt() is a void method so s.setInt(i) returns 0.

"Out of segment space" means a segment index is out of range.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: What is wrong with the invocation "Output.printString(s.setInt(i))" ?

Rather Iffy
This post was updated on .
Thnx Mark.

I was under the wrong impression that a method call on an object ALWAYS somehow modifies that object AND returns it.
I see from your remark that it is not the case with s.setInt().

Do you know why the return value of setInt() of the String API is void and not String ?

--Chrit
Reply | Threaded
Open this post in threaded view
|

Re: What is wrong with the invocation "Output.printString(s.setInt(i))" ?

cadet1620
Administrator
Rather Iffy wrote
I was under the wrong impression that a method call on an object ALWAYS somehow modifies that object AND returns it.
I see from your remark that it not the case with s.setInt().

Do you know why the return value of setInt() of the String API is void and not String ?
String is the only method in the Hack OS that has any methods other than dispose().

s.dispose() clearly cannot return the object it just deleted.

Methods like s.length() do not modify the object and must return something that isn't the object.

All String's methods that don't return info about the String or its content, including all the methods that change the String are void functions.  This is a stylistic choice by the object library's designers.  

There is one exception: s.AppendChar() returns the String object to simplify the Jack Compiler's string constant code.

--Mark