The Crystal Programming Language Forum

Synonyms for logical operators (not, and, or)

Hi
I prefer using the words not, or, and instead of !, ||, &&. Personally i think they are easier to type and read.
Is there a simple way to do this myself?
using compile time string substitution
or
reopening a class and adding three instance methods
or
any other solution?

Thanks

Hi!

No. The only way is forking the compiler.

Or you can use Nim (maybe others), which uses the conventions you want.

You could do this:

def not(thing)
  !thing
end

puts(not true)

class Object
  def or(other)
    self || other
  end
  
  def and(other)
    self && other
  end
end

puts false.or true

puts "hi".and "hello"

It looks funny, and probably doesn’t work in all cases, but that’s about as close as you might get :joy:

1 Like