Open Yuri05 opened 1 year ago
It's not a bug, it's a feature :)
The function tests if "An object or an atomic vector or a list of objects" is empty. ""
is a vector of length 1 with one entry - the empty string.
> length("sd")
[1] 1
> length("")
[1] 1
Furthermore, ""
is a valid unit string for the dimension Dimensionless
, so its not empty...
well, in the most languages isempty(<empty string>)
returns true
.
e.g. in Matlab, isempty returns true for both empty array and empty string:
But if everybody is fine with the current implementation - I can live with it.
I would assume it's a bug. An empty string is always empty In which scenario does someone checks is empty ("") and does something meaningful when it's true? Can you explain @PavelBal ?
And isEmpty for the "" unit should reurnr true so that we don't put [] for nothing
isEmpty()
checks if the object is empty. In R's world, ""
is a vector of type character
with one element - an empty string. We can even access this first element:
> ""[[1]]
[1] ""
An empty objects/structure would be character()
:
> isEmpty(character())
[1] TRUE
And we indeed cannot access the first element:
> c <- character()
> c[[1]]
Error in c[[1]] : subscript out of bounds
And while I agree that this may be not intuitive, the behavior of this function is correct from R's perspective and I would not change it. You can read isEmpty()
in the way "are there elements that I can subset?", and the answer for ""
is "Yes".
what about we create a method areThereElementsToSubset ?
Or isEmptyString()
? :) In this case we would not change the current implementation and potentially break some dependencies.
I guess what you have in mind is nchar()
:
nchar("")
#> [1] 0
Created on 2022-12-11 with reprex v2.0.2
Btw, if you plan to create a new function isEmptyString()
, you can repurpose the existing logic from (vectorized) hasEmptyStrings()
: