Re: Constructors?
Byron Foster <[email protected]> Wed, 06 Oct 2004 03:08:19 -0700
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
Francis Barber wrote:
> Byron Foster wrote:
>> I was wonder why it is necessary to call a constructor from within
>> an exsisting constructor. I found the following to be confusing
>> when trying to define a default constructor.
>>
>> public class Test { }
>>
>> new Test() { println ("Constructing Test, 123"); this(); }
>>
>> The above will not compile with the following error:
>>
>> nice.lang: parsing foo: parsing foo: typechecking
>>
>> ~/nice/./foo/Test.nice: line 8, column 3: Ambiguity for symbol
>> this. Possibilities are : foo.Test <init>() foo.Test <init>()
>>
>> I assume this is occuring because there is an automatic constructor
>> and the Test() constructor that I have now defined. However, this
>> seems to be a likely case.
>>
>> Also, Is it possible to declare a constructor within the class
>> definition?
>
> I'm not the expert on this topic, but the way I see it the
> constructor you have defined is actually calling itself recursively.
> As there is no base case, the recursion will continue indefinitely
> and so object of type Test can ever be instantiated. I imagine this
> is illegal in most, if not all, object oriented languages. The
> problem here is probably that the Nice error message is misleading,
> to say the least. This is what Java has to say about it.
>
> public class Test { public Test() { this();
> System.out.println("Hello"); } }
>
> javac Test.java
>
> Test.java:3: Recursive constructor invocation: Test(). this();
>
> I'm not sure about the last question: "Also, Is it possible to
> declare a constructor within the class definition?" What I would
> say, though, is that Nice's default constructor is pretty powerful
> and it is often unnecessary to define your own. Don't forget that
> you can use an initialisation block if you need to. See
> http://nice.sourceforge.net/manual.html#constructor and
> http://nice.sourceforge.net/manual.html#id2429177
Arjan provided the following response to this same question I posted on
the sourceforge forums:
http://sourceforge.net/forum/forum.php?thread_id=1155614&forum_id=40268
There seems to be some discussion surrounding this issue. After reading
the discussions I think I understand the design decisions better.
Certainly given the fact that one cannot access member variables from
within a constructor I guess it wouldn't make much since not to call the
automatic constructor, or some chain of constructors that end with an
automatic constructor. Otherwise, nothing in the instance changes. I
wonder if the Nice constructor approach aids in better compilation and
object creation performance, or if it is strictly language motivated?
-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl