Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type checking for base string functions #22

Open
alistaire47 opened this issue Jun 5, 2017 · 0 comments
Open

Type checking for base string functions #22

alistaire47 opened this issue Jun 5, 2017 · 0 comments

Comments

@alistaire47
Copy link

alistaire47 commented Jun 5, 2017

Base R's internal string functions (regex, strsplit, substr, paste, etc.) all coerce non-string types (particularly factors, but also numbers) to character vectors, e.g.

# Nice ordered factor
mon <- factor(month.name, month.name, ordered = TRUE)
mon
#>  [1] January   February  March     April     May       June      July     
#>  [8] August    September October   November  December 
#> 12 Levels: January < February < March < April < May < June < ... < December

# Implicitly coerces factor to character
substr(mon, 1, 3)
#>  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
#> [12] "Dec"

# Operating on levels keeps types properly
levels(mon) <- substr(levels(mon), 1, 3)
mon
#>  [1] Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec

or more ridiculously

gsub(12L, 21, 1234L)
#> [1] "2134"

While the coercion is at least consistent and probably expected by anyone who has used R for a while, it would occasionally be convenient to have a stricter type-safety requirement whereby all coercion must be explicit (like strict does with apply on data.frames), making R

paste0('foo', 47L)
# Error: [strict] ...

paste0('foo', as.character(47L))
#> [1] "foo47"

more like Python:

print('foo' + 47)
# TypeError: must be str, not int

print('foo' + str(47))
#> foo47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant