Jack Constructors as normal Functions

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

Jack Constructors as normal Functions

mmukov
Why not remove the special constructor subroutine and replace it with a normal function ? :

class Point
{
        field int mX, mY;

        method void cons ( int x, int y )
        {
                let mX = x;
                let mY = y;
                return;
        }

        function Point make ( int x, int y )
        {
                var Point p;
                let p = Memory.alloc( 2 );
                do p.cons( x, y );
                return p;
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: Jack Constructors as normal Functions

ivant
What will happen if you add a third field, but forget to fix the call to Memory.alloc()?
Reply | Threaded
Open this post in threaded view
|

Re: Jack Constructors as normal Functions

mmukov
This is a very "weak" argument ... You can forget many things. You can forget to dispose a field, if you change it from int to object type ... The goal of the course is declared to be: "to create just enough functionality, so the essence can be captured". The constructor is a not-needed magical part. The proposed implementation is revealing to the undelying mechanics and have a good pedagogial value.
Reply | Threaded
Open this post in threaded view
|

Re: Jack Constructors as normal Functions

ivant
It is not a weak argument. You can have several constructors in one class. You'll need to make sure they are all allocate the correct amount of memory during the development. This means having to hunt down countless hard to find errors.

I think you shouldn't take the "just enough functionality" that literally. I'm sure there are other examples where the authors added not-strictly needed functionality for various reasons.