Skip to content
Prev 55718 / 63421 Next

ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1

Okay, I thought you always wanted to check the length, but if we can only check what's evaluated I mostly agree.

I still think there's not much wrong with how length-0 logicals are treated, as the return of NA in cases where the value matters is enough warning I think, and I can imagine some code like my previous example 'x==-1 || length(x)==0', which wouldn't need a warning.

But we could do a check for length being >1

Greetings, Emil


?On 30/08/2018, 14:55, "R-devel on behalf of Joris Meys" <r-devel-bounces at r-project.org on behalf of jorismeys at gmail.com> wrote:
On Thu, Aug 30, 2018 at 2:09 PM D?nes T?th <toth.denes at kogentum.hu> wrote:
> Note that `||` and `&&` have never been symmetric:
    >
    > TRUE || stop() # returns TRUE
    > stop() || TRUE # returns an error
    >
    >
    Fair point. So the suggestion would be to check whether x is of length 1
    and whether y is of length 1 only when needed. I.e.
    
    c(TRUE,FALSE) || TRUE
    
    would give an error and
    
    TRUE || c(TRUE, FALSE)
    
    would pass.
    
    Thought about it a bit more, and I can't come up with a use case where the
    first line must pass. So if the short circuiting remains and the extra
    check only gives a small performance penalty, adding the error could indeed
    make some bugs more obvious.
    
    Cheers
    Joris
    
    -- 
    Joris Meys
    Statistical consultant
    
    Department of Data Analysis and Mathematical Modelling
    Ghent University
    Coupure Links 653, B-9000 Gent (Belgium)
    <https://maps.google.com/?q=Coupure+links+653,%C2%A0B-9000+Gent,%C2%A0Belgium&entry=gmail&source=g>
    
    -----------
    Biowiskundedagen 2017-2018
    http://www.biowiskundedagen.ugent.be/
    
    -------------------------------
    Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
    
    
    ______________________________________________
    R-devel at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/r-devel

Thread (15 messages)

Henrik Bengtsson ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 28 Emil Bode ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Joris Meys ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Tóth Dénes ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Tóth Dénes ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Joris Meys ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Emil Bode ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Hadley Wickham ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 William Dunlap ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Martin Maechler ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Rui Barradas ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Joris Meys ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Hadley Wickham ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 30 Emil Bode ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 31 Henrik Bengtsson ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 Aug 31