Re: Constructors?

Francis Barber <[email protected]> Wed, 6 Oct 2004 11:06:01 +1000 (EST)
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
Hello Byron,

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

Regards,
Frank.

> 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?
> 



Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com


-------------------------------------------------------
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