From Basil.Abou-El-Komboz at stat.uni-muenchen.de  Tue Sep 11 22:04:13 2012
From: Basil.Abou-El-Komboz at stat.uni-muenchen.de (Basil Abou El-Komboz)
Date: Tue, 11 Sep 2012 22:04:13 +0200
Subject: [Rd] R crashes when printing a named numeric vector of a
	specific class - Bug?
In-Reply-To: <CAAmySGPZu8fR3+paPs=Hii_EL7UmUy6vMPN8DWQnUtKszx0d7Q@mail.gmail.com>
	(R. Michael Weylandt's message of "Tue, 11 Sep 2012 16:52:00 +0100")
References: <soutxv4r4p2.fsf@gmx.de> <1347377748.1805.41.camel@milan>
	<CAAmySGPZu8fR3+paPs=Hii_EL7UmUy6vMPN8DWQnUtKszx0d7Q@mail.gmail.com>
Message-ID: <soubohc5nsy.fsf@gmx.de>


    >> Le mardi 11 septembre 2012 ? 16:53 +0200, Basil Abou El-Komboz a
    >> ?crit :
    >>> Dear useR's,
    >>> 
    >>> today I stumbled over an interesting phenomenon: First, I
    >>> created a named numeric vector with a certain class and several
    >>> attributes via the structure() function. After that, I
    >>> implemented a simple print method for this class. When calling
    >>> this function it produces an endless loop of print calls until R
    >>> crashes. :/
    >>> 
    >>> What is going on here? Is this a bug or have I done something
    >>> completely wrong? :)
    >>> 
    >>> Below is a minimal example which reproduces the behavior. Be
    >>> careful when calling foo() as this automatically calls
    >>> print.bar() which causes R to crash (at least on my PC, see
    >>> further informations about my system below.)
    >>> 
    >>> Greetings, Basil
    >>> 
    >>> --------------------------------------------------
    >>> 
    >>> Minimal example:
    >>> 
    >>> foo <- function () { x <- c("A" = 1.3, "B" = 0.7, "C" = -0.3)
    >>> structure(x, class = "bar") }
    >>> 
    >>> print.bar <- function (x, ...) { print(x, ...)  }
    >> What is your code supposed to do exactly? ;-)
    >> 
    >> You're calling print() in your class' print.bar() function, so
    >> calling print() on such an object will call print.bar(), which
    >> calls print(), which calls print.bar()... In a few moments the
    >> recursion will have gone so deep that some system limit about the
    >> stack size must be reached, and R is killed.
    >> 
    >> If you just want to print the object as a vector, you do not need
    >> to define any function. Or, at least, call print.default()
    >> instead of the generic print().
    >> 
    >> 
    >> My two cents
    >> 

    R> NextMethod() may also be of some help here, depending on the
    R> inheritance you're envisioning.

    R> Michael

Oops, I was blind. Thanks for the answers and pointing me in the right
direction. Of course, this is not a problem of R but a problem of my
coding. ;)

Greetings,
Basil



[Rd] R crashes when printing a named numeric vector of a specific class - Bug?
Basil Abou El-Komboz