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

Glossary returning unanticipated definitions, if leading characters match #338

Open
michaelmcd18 opened this issue Jun 12, 2024 · 4 comments

Comments

@michaelmcd18
Copy link

Found this case while using the new read_glossary() function. Here's the example case:

The glossary.tex file contains definitions for ng and MCMC. It does NOT have definitions for n or MC. However, I get the following:

> glossary$n
nanograms (ng)
> glossary$MC
Markov chain Monte Carlo (MCMC)

It seems if the leading characters match an existing definition it returns that definition. I'd expect an error if there wasn't an exact match.

@kylebaron
Copy link
Contributor

Thanks for the report, @michaelmcd18. Lists do partial matching by default. I can turn this off for glossary objects.

x <- list(michael = "scientist")

x$mi
#> [1] "scientist"

Created on 2024-06-12 with reprex v2.0.2

@kyleam
Copy link
Contributor

kyleam commented Jun 12, 2024

[ I see @kylebaron just posted, but I'll post this reply just for additional info. I'm not against turning it off. ]

Related to discussion at #326 (comment)

It seems if the leading characters match an existing definition it returns that definition. I'd expect an error if there wasn't an exact match.

Hmm, I'm not a fan of the partial match with $ (and I like tibble's departure from it), but I don't think it's altogether surprising given the standard $ behavior:

x <- list(foo = "foo", bar = "bar")

x$fo
#> [1] "foo"

options(warnPartialMatchDollar = TRUE)
x$fo
#> Warning in x$fo: partial match of 'fo' to 'foo'
#> [1] "foo"

One way to avoid it is with [[ and then a check for null:

x[["fo"]]
#> NULL

@kylebaron
Copy link
Contributor

I took care of this in yspec, which is similarly a list of lists; can handle it the same way here.

https://github.com/metrumresearchgroup/yspec/blob/main/R/class-yspec.R#L78-L84

@michaelmcd18
Copy link
Author

Thanks for the explanations @kylebaron @kyleam! I certainly don't want to add extra work, it seems like if I was using [[ I wouldn't have run into it at all, so I'm good either way with keeping or removing the partial matching

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

3 participants