I am trying to create a function that checks for strict equality and I would like to use the triple equal sign. Some context:
> 3 == '3'
[1] TRUE
> FALSE == 0
[1] TRUE
All of the above check returns TRUE
because the inputs are coerced to a common type. However I want to check for strict equality. The identical
function does exactly what I need.
> identical(3,'3')
[1] FALSE
> identical(FALSE, 0)
[1] FALSE
Now I want to implement this in a more concise and less verbose way. As in Javascript I would like to use the triple equal sign. I wrote this function:
`===` <- function(a,b){
identical(a,b)
}
However this doesn't behave as expected:
> 3 === 3
Error: unexpected '=' in "3 ==="
What am I missing? Thank you
You can define an infix operator (with the e
in %e%
for "equal";-):
`%e%` <- function(a, b) identical(a, b)
3 %e% 3
#[1] TRUE
Or if you want the triple-equal sign as
`%===%` <- function(a, b) identical(a, b)
3 %===% 3
#[1] TRUE
Or an example with vectors
1:3 %===% 1:3
#[1] TRUE
These infix operations (where the operator is used between the operands) can also be written as
`%===%`(1:3, 1:3)
in the same way that you can write
`==`(3, 3)