20 May 2004 09:29
Re: [groovy-dev] Static factory method
On 20 May 2004, at 08:25, John Wilson wrote: > On 19 May 2004, at 18:46, jastrachan@... wrote: >> On 19 May 2004, at 18:33, Samuel Pullara wrote: >>> If there is going to be a static method on classes that makes new >>> instances, I think it should be called newInstance() like the >>> equivalent reflective constructor invocations. >> >> I like that too - it was my first thought. The tricky bit is how to >> identify the 'real' constructor method. Maybe new()? >> >> lass Point { >> static Point newInstance(x, y) { >> if (x == 0 & y == 0) { >> return origin >> } >> else { >> // really do the creation >> return new(x, y) >> } >> } >> } >> > > What about just declaring the constructor static? > > Class Point { > static Point (x, y) { > if (x == 0 & y == 0) { > return origin > } > else { > // really do the creation > return new Point(x, y) > } > } > > Point(x, y) { > // do the actual construction > } > } But inside the static constructor - how can you tell the difference between calling the static constructor and calling the 'real' constructor. i.e. at the jvm level, to disambiguate between Point.Point() and new Point(). James ------- http://radio.weblogs.com/0112098/
RSS Feed