nealrichardson commented on code in PR #12817:
URL: https://github.com/apache/arrow/pull/12817#discussion_r852398025


##########
r/R/type.R:
##########
@@ -68,17 +69,50 @@ FLOAT_TYPES <- c("float16", "float32", "float64", 
"halffloat", "float", "double"
 #' type(c("A", "B", "C"))
 #' type(mtcars)
 #' type(Sys.Date())
+#' type(as.POSIXlt(Sys.Date()))
+#' type(vctrs::new_vctr(1:5, class = "my_custom_vctr_class"))
 #' @export
-type <- function(x) UseMethod("type")
+type <- function(x) infer_type(x)
 
+#' @rdname type
 #' @export
-type.default <- function(x) Array__infer_type(x)
+infer_type <- function(x, ...) UseMethod("infer_type")
 
 #' @export
-type.ArrowDatum <- function(x) x$type
+infer_type.default <- function(x, ..., from_array_infer_type = FALSE) {
+  # If from_array_infer_type is TRUE, this is a call from C++ and there was
+  # no S3 method defined for this object.
+  if (from_array_infer_type) {
+    abort(
+      sprintf(
+        "Can't infer Arrow data type from object inheriting from %s",
+        paste(class(x), collapse = " / ")
+      )
+    )
+  }
+
+  # If from_array_infer_type is FALSE, this is a user calling type() from R
+  # and we to call into C++. If there is no built-in conversion for this
+  # object type, C++ will call back here with from_array_infer_type = TRUE
+  # to generate a nice error message.
+  Array__infer_type(x)
+}
+
+#' @export
+infer_type.ArrowDatum <- function(x, ...) x$type

Review Comment:
   This isn't an inference though. (Not a reason not to do this, just observing)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to