Skip to content

Commit

Permalink
Allow coercions between complex and numeric types (#673)
Browse files Browse the repository at this point in the history
Closes #564
  • Loading branch information
lionel- committed Nov 18, 2019
1 parent b50e5ee commit 7547955
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions NAMESPACE
Expand Up @@ -295,6 +295,7 @@ S3method(vec_proxy_equal,default)
S3method(vec_ptype2,Date)
S3method(vec_ptype2,POSIXt)
S3method(vec_ptype2,character)
S3method(vec_ptype2,complex)
S3method(vec_ptype2,data.frame)
S3method(vec_ptype2,default)
S3method(vec_ptype2,difftime)
Expand Down Expand Up @@ -323,12 +324,17 @@ S3method(vec_ptype2.character,default)
S3method(vec_ptype2.character,factor)
S3method(vec_ptype2.character,ordered)
S3method(vec_ptype2.character,vctrs_unspecified)
S3method(vec_ptype2.complex,complex)
S3method(vec_ptype2.complex,default)
S3method(vec_ptype2.complex,double)
S3method(vec_ptype2.complex,integer)
S3method(vec_ptype2.data.frame,data.frame)
S3method(vec_ptype2.data.frame,default)
S3method(vec_ptype2.data.frame,vctrs_partial_frame)
S3method(vec_ptype2.difftime,default)
S3method(vec_ptype2.difftime,difftime)
S3method(vec_ptype2.difftime,vctrs_unspecified)
S3method(vec_ptype2.double,complex)
S3method(vec_ptype2.double,default)
S3method(vec_ptype2.double,double)
S3method(vec_ptype2.double,integer)
Expand All @@ -340,6 +346,7 @@ S3method(vec_ptype2.factor,factor)
S3method(vec_ptype2.factor,ordered)
S3method(vec_ptype2.factor,vctrs_partial_factor)
S3method(vec_ptype2.factor,vctrs_unspecified)
S3method(vec_ptype2.integer,complex)
S3method(vec_ptype2.integer,default)
S3method(vec_ptype2.integer,double)
S3method(vec_ptype2.integer,integer)
Expand Down Expand Up @@ -525,6 +532,7 @@ export(vec_ptype2)
export(vec_ptype2.Date)
export(vec_ptype2.POSIXt)
export(vec_ptype2.character)
export(vec_ptype2.complex)
export(vec_ptype2.data.frame)
export(vec_ptype2.difftime)
export(vec_ptype2.double)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
@@ -1,6 +1,9 @@

# vctrs 0.2.0.9000

* Complex numbers are coercible with the integer and
double (#564).

* zeallot has been moved from Imports to Suggests, meaning that `%<-%` is no
longer re-exported from vctrs.

Expand Down
27 changes: 27 additions & 0 deletions R/type-bare.R
Expand Up @@ -17,6 +17,11 @@ vec_ptype2.integer <- function(x, y, ...) UseMethod("vec_ptype2.integer", y)
#' @method vec_ptype2 double
#' @export
vec_ptype2.double <- function(x, y, ...) UseMethod("vec_ptype2.double", y)
#' @rdname vec_ptype2
#' @export vec_ptype2.complex
#' @method vec_ptype2 complex
#' @export
vec_ptype2.complex <- function(x, y, ...) UseMethod("vec_ptype2.complex", y)

#' @method vec_ptype2.logical logical
#' @export
Expand Down Expand Up @@ -48,6 +53,23 @@ vec_ptype2.integer.double <- function(x, y, ...) shape_match(double(), x, y)
#' @method vec_ptype2.double integer
vec_ptype2.double.integer <- function(x, y, ...) shape_match(double(), x, y)

#' @export
#' @method vec_ptype2.complex complex
vec_ptype2.complex.complex <- function(x, y, ...) shape_match(complex(), x, y)
#' @export
#' @method vec_ptype2.integer complex
vec_ptype2.integer.complex <- function(x, y, ...) shape_match(complex(), x, y)
#' @export
#' @method vec_ptype2.complex integer
vec_ptype2.complex.integer <- function(x, y, ...) shape_match(complex(), x, y)
#' @export
#' @method vec_ptype2.double complex
vec_ptype2.double.complex <- function(x, y, ...) shape_match(complex(), x, y)
#' @export
#' @method vec_ptype2.complex double
vec_ptype2.complex.double <- function(x, y, ...) shape_match(complex(), x, y)



# Character

Expand Down Expand Up @@ -123,6 +145,11 @@ vec_ptype2.integer.default <- function(x, y, ..., x_arg = "x", y_arg = "y") {
vec_ptype2.double.default <- function(x, y, ..., x_arg = "x", y_arg = "y") {
vec_default_ptype2(x, y, x_arg = x_arg, y_arg = y_arg)
}
#' @method vec_ptype2.complex default
#' @export
vec_ptype2.complex.default <- function(x, y, ..., x_arg = "x", y_arg = "y") {
vec_default_ptype2(x, y, x_arg = x_arg, y_arg = y_arg)
}
#' @method vec_ptype2.character default
#' @export
vec_ptype2.character.default <- function(x, y, ..., x_arg = "x", y_arg = "y") {
Expand Down
3 changes: 3 additions & 0 deletions man/vec_ptype2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat/test-type-bare.R
Expand Up @@ -219,6 +219,26 @@ test_that("Shaped NA casts work as expected", {
expect_equal(vec_cast(mat(list(NA)), to_mat), exp_mat)
})

test_that("complex is coercible to numeric types", {
expect_identical(vec_ptype2(cpl(), NULL), cpl())
expect_identical(vec_ptype2(NULL, cpl()), cpl())

expect_identical(vec_ptype2(cpl(), int()), cpl())
expect_identical(vec_ptype2(int(), cpl()), cpl())

expect_identical(vec_ptype2(cpl(), dbl()), cpl())
expect_identical(vec_ptype2(dbl(), cpl()), cpl())

expect_identical(vec_ptype2(cpl(), cpl()), cpl())

expect_identical(vec_c(0, 1i), cpl(0i, 1i))
})

test_that("complex is not coercible to logical", {
expect_error(vec_ptype2(cpl(), lgl()), class = "vctrs_error_incompatible_type")
expect_error(vec_ptype2(lgl(), cpl()), class = "vctrs_error_incompatible_type")
})


# Character

Expand Down

0 comments on commit 7547955

Please sign in to comment.