mllg / checkmate

Fast and versatile argument checks
https://mllg.github.io/checkmate/
Other
261 stars 29 forks source link

let type checks report actual *base* type, not classes #130

Open maxheld83 opened 6 years ago

maxheld83 commented 6 years ago
x <- structure(.Data = 1L, class = c("foo", "bar"))

check_character(x = x)
# [1] "Must be of type 'character', not 'foo/bar'"

str(x)
# Classes 'foo', 'bar'  int 1

typeof(x)
# [1] "integer"

I find the above error message a little bit misleading; it reports the class of the offending input, though I expected the type from the first part of the error message.

This could be a little confusing to the user; in this case, classes c("foo", "bar") are actually not the problem, but the integer base type.

Maybe typeof() would be more intuitive here?

mllg commented 6 years ago

You are right that the message is not 100% to the point. However, using typeof() is problematic. If you attach a class to an atomic, you usually want it to be treated differently, independent of the underlying atomic structure. Many base types are constructed by just adding an attribute like class:

I will try to improve the error message though.