0

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

Marco De Virgilis
  • 982
  • 1
  • 9
  • 29
  • 1
    I don’t know anything about R but I would guess that its syntax doesn’t allow a `===` operator? – mkrieger1 Mar 03 '20 at 21:45
  • 1
    If you want to create your own infix equality operator, you'll need to use `%%` as Maurits pointed out. The R tokenizer/parser will not recognize symbols not otherwise defined in the language specification. – MrFlick Mar 03 '20 at 21:51
  • @MrFlick Somehow `data.table` managed to define `:=`; I remember reading about this at some point in time but can't remember any details. Do you know how `data.table` goes about defining a new infix(-like?) operator without the `%`s? – Maurits Evers Mar 03 '20 at 22:00
  • 1
    @MauritsEvers That's not quite true. `:=` just happened to already be defined in the parser (a left over operator that was planned to be used but never got a default definition). If they wanted to use anything else, like `~=` or `@=` that would not have been possible. They just got "lucky" there was a left over operator that the parser would recognize. You can't create your own without changing the source code for R itself. – MrFlick Mar 03 '20 at 22:03
  • @MrFlick Interesting. I didn't know that. – Maurits Evers Mar 03 '20 at 22:04
  • 1
    Related to `:=`: https://stackoverflow.com/questions/26269423/why-is-allowed-as-an-infix-operator/26269553#26269553 – MrFlick Mar 04 '20 at 04:15

1 Answers1

6

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)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • I know I could have used percentage signs but it kinda defeats the purpose of typing it fast. At least on my keyboard is shift + 5. I know I could remap the keyboard but it would be an overkill at this point. Is there a way to just use the equal signs? – Marco De Virgilis Mar 03 '20 at 21:53
  • 1
    @MarcoDeVirgilis I see. No, I don't think you can avoid the `%` signs when using custom infix operators. If you define it as `%e%` at least you'll have the same number of keys as `===`;-) – Maurits Evers Mar 03 '20 at 21:55
  • 2
    @MarcoDeVirgilis Perhaps interesting in the wider context: [Why was % chosen as the operator delimiter](https://community.rstudio.com/t/why-was-chosen-as-the-operator-delimiter/3309) – Maurits Evers Mar 03 '20 at 21:57