Benjamin Tyner
2010-Sep-15 01:23 UTC
[Rd] warning or error upon type/storage mode coercion?
Hi, I'm aware that the language definition states "R objects are often coerced to different types during computations". Two questions: 1. Is it possible to configure the R environment so that, for example, coercion from (say) numeric to integer will throw a warning or an error? I realize that in the base R code alone, there are thousands of calls to as.integer() which would trigger such an event, so this would not be a very practical configuration... 2. So, assuming the answer to (1) is a resounding "no", does anyone care to state an opinion regarding the philosophical or historical rationale for why this is the case in R/S, whereas certain other interpreted languages offer the option to perform strict type checking? Basically, I'm trying to explain to someone from a perl background why the (apparent) lack of a "use strict; use warnings;" equivalent is not a hindrance to writing bullet-proof R code. Thanks, Ben
Henrik Bengtsson
2010-Sep-15 04:18 UTC
[Rd] warning or error upon type/storage mode coercion?
On Tue, Sep 14, 2010 at 6:23 PM, Benjamin Tyner <btyner at gmail.com> wrote:> Hi, > > I'm aware that the language definition states "R objects are often coerced > to different types during computations". Two questions: > > 1. Is it possible to configure the R environment so that, for example, > coercion from (say) numeric to integer will throw a warning or an error? I > realize that in the base R code alone, there are thousands of calls to > as.integer() which would trigger such an event, so this would not be a very > practical configuration... > > 2. So, assuming the answer to (1) is a resounding "no", does anyone care to > state an opinion regarding the philosophical or historical rationale for why > this is the case in R/S, whereas certain other interpreted languages offer > the option to perform strict type checking? Basically, I'm trying to explain > to someone from a perl background why the (apparent) lack of a "use strict; > use warnings;" equivalent is not a hindrance to writing bullet-proof R code.For what's it's worth: it is only recently (only some R releases ago) that the language/parser gained the syntax for specifying an integer, e.g. 2L. I guess, before this the only option you had to get an integer was through coercion, e.g. as.integer(2), storage.mode(), but also tricks such as 2:2. So in some sense from the parsers point of view, everything was doubles in the beginning. (disclaimer: I might be missing something). My $.02 /Henrik PS. OT, but reading help(":") I just learned that a:b is not always the same as rev(b:a).> > Thanks, > Ben > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >
On 15 Sep 2010, at 03:23, Benjamin Tyner wrote:> 2. So, assuming the answer to (1) is a resounding "no", does anyone care to state an opinion regarding the philosophical or historical rationale for why this is the case in R/S, whereas certain other interpreted languages offer the option to perform strict type checking? Basically, I'm trying to explain to someone from a perl background why the (apparent) lack of a "use strict; use warnings;" equivalent is not a hindrance to writing bullet-proof R code.If they're from a Perl background, you might also want to point out to them that (base) Perl doesn't do _any_ type checking at all, and converts types as needed. As in ... $x = "0.0"; if ($x) ... # true if ($x+0) ... # false AFAIK, that's one of the main complaints that people have about Perl. "use strict" will just make sure that all variables have to be declared before they're used, so you can't mess up by mistyping variable names. Which is something I'd very much like to have in R occasionally ... Best, Stefan
Karl Forner
2010-Sep-15 08:15 UTC
[Rd] Fwd: warning or error upon type/storage mode coercion?
---------- Forwarded message ---------- From: Karl Forner <karl.forner@gmail.com> Date: Wed, Sep 15, 2010 at 10:14 AM Subject: Re: [Rd] warning or error upon type/storage mode coercion? To: Stefan Evert <stefanML@collocations.de> I'm a Perl fan, and I really really miss the "use strict" feature. IMHO it's very error-prone not to have thios safety net. Best, On Wed, Sep 15, 2010 at 9:54 AM, Stefan Evert <stefanML@collocations.de>wrote:> > On 15 Sep 2010, at 03:23, Benjamin Tyner wrote: > > > 2. So, assuming the answer to (1) is a resounding "no", does anyone care > to state an opinion regarding the philosophical or historical rationale for > why this is the case in R/S, whereas certain other interpreted languages > offer the option to perform strict type checking? Basically, I'm trying to > explain to someone from a perl background why the (apparent) lack of a "use > strict; use warnings;" equivalent is not a hindrance to writing bullet-proof > R code. > > If they're from a Perl background, you might also want to point out to them > that (base) Perl doesn't do _any_ type checking at all, and converts types > as needed. As in ... > > $x = "0.0"; > if ($x) ... # true > if ($x+0) ... # false > > AFAIK, that's one of the main complaints that people have about Perl. "use > strict" will just make sure that all variables have to be declared before > they're used, so you can't mess up by mistyping variable names. Which is > something I'd very much like to have in R occasionally ... > > Best, > Stefan > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]