class constructor return type

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

class constructor return type

eXtc_be
Does a constructor have to return this?

I would have sworn I read it somewhere in the book, but I couldn't find it. The only thing that comes close is on page 95 right before Figure 9.5: Therefore the type of a constructor must always be the name of the class to which it belongs.

The reason I ask is because I implemented my parser so it quits with an error message if the return type in the constructor's declaration doesn't match the class name, or if it doesn't return this. Parsing ExpressionLessSquare\Square.jack and ExpressionLessSquare\SquareGame.jack doesn't complete because they contain constructors that don't return this. If I change the code to return this my parser finishes without a problem, but then the Text Comparer throws a hissy fit.

If I disable this extra check, the files in ExpressionLessSquare pass, but it doesn't feel right. Besides, the constructors in Square and ArrayTest do return this like they should.

Does anyone have an opinion on this? (pun not intended ;)
Reply | Threaded
Open this post in threaded view
|

Re: class constructor return type

ivant
I don't remember the details from the book, but I think the Jack source code of the constructor does not need to return anything. The implementation of the constructor - the VM code generated by the compiler, should return this automatically.
Reply | Threaded
Open this post in threaded view
|

Re: class constructor return type

eXtc_be
Thank you for your response. I did not yet read the next chapter, so I didn't know that.