recursion - Is this a bug? (recursive constructors in Java) -


i've been playing recursive constructors in java. following class accepted compiler 2 examples of recursive constructors in java. crashes stackoverflowerror @ runtime using java 1.7.0_25 , eclipse juno (version: juno service release 2 build id: 20130225-0426).

  class mylist<x> {     public x hd;     public mylist<x> tl;      public mylist(){       this.hd = null;       this.tl = new mylist<x>();     }   } 

the error message makes sense, i'm wondering if compiler should catch it. counterexample might list of integers constructor takes int argument , sets this.tl null if argument less zero. seems reasonable allow in same way recursive methods allowed, on other hand think constructors ought terminate. should constructor allowed call itself?

so i'm asking higher authority before submitting java bug report.

edit: i'm advocating simple check, prohibiting constructor calling or whatever java developers did address https://bugs.openjdk.java.net/browse/jdk-1229458. wilder solution check arguments recursive constructor calls decreasing respect well-founded relation, point of question not "should java determine whether constructors terminate?" rather "should java use stronger heuristic when compiling constructors?".

you have several constructors different parameters, calling each other wiht this(...). in general, computer science, termination of code can not guaranteed. intelligence, in simple case, nice have, 1 may not require compiler error. a bit unreachable code. there no difference between constructor or normal method in eyes however.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -