3

While browsing my code in a weakly-typed language I was seeing that I've trained myself to use identity (===) where logical. Then I came across a greater (or less) than or equal to (>=), and it made me wonder... why is there no "greater than or identical to"? I suppose it would be >==. For example...

5 == 5    // true
5 === 5   // true
5.5 >= 5  // true
5.5 >== 5 // false
6 >= 5    // true
6 >== 5   // true

Basically, I would throw a false if it was of a different type. For example, if I want to check if $x is greater than $y, but I want them both to be integers (or floats, but no mixing), then wouldn't it make sense to have a single call that can do all that, rather than having to check separately to see if they were the same type?

A quick google indicated that this may not exist in any language; why not? Is it just not as useful as I might think it is? :)

14
  • 1
    @Frustrated: I think because 5.5 is floating point type and 5 is an integer type, and the identity operator returns false if two values are different types.
    – Matt H
    Jan 27, 2011 at 21:48
  • 1
    @Matt: That sort of makes sense... but that means that the hypothetical >== is doing two things: testing that the two operands and if they are, compare values of them. That is confusing and I don't like it. Would >== only be applicable to numeric operands? What if I tried $shoppingcart >== $user_acct_info? Would that even make sense? I think that $shoppingcart === $user_acct_info might make sense (I think it would test if the two are of the same type), but >==? ... I dunno man... Jan 27, 2011 at 21:52
  • 1
    Isn't === doing two things, though? It tests that the two operands are identical and, if they are, compares the values.
    – Andrew
    Jan 27, 2011 at 21:55
  • 1
    @Frustrated, I don't understand your last comment.
    – Andrew
    Jan 27, 2011 at 21:59
  • 2
    @Andrew: === does one thing - tests if two things are identical including their types. Your proposed >== tests if the types are identical and if so compares them. If this is a valid operation, then so is "test if they are the same type and if so, check if one is strictly greater than the other", and how would you notate that?
    – Anon.
    Jan 27, 2011 at 22:11

2 Answers 2

1

At first glance it does seem like this is an inadequacy in a language that uses === (examples are in JavaScript):

5 == "5" // true
5 === "5" // false
5 >= "5" // true

However, two things:

  1. In PHP, there are few if any instances in which a string number isn't converted automatically into a number type. (See my answer about casting in PHP)

  2. Using === with numbers isn't really that useful - after all, implicit conversion is a feature of weak typing that is usually desirable. The much more common usage is making sure you getting the right comparison with all the "falsey" values -- null, undefined, 0, false, "".

1
  • Thanks for the link and the answer. I personally use explicit typecasting only as a method for sanitizing for database input; if I know that the field expects an int, I can simply cast it to int rather than go through the escaping and quoting business. And yes, I try to avoid using === for numbers because sometimes a query or form might have inserted a numeric string into the process. Or, sometimes when I'm passing it to something expecting a boolean. But as you said, typecasting "down".
    – Andrew
    Jan 28, 2011 at 14:12
4

I think that such an operator would be too confusing by mixing the concepts of logical ordering and object identity. A proper implementation of >= and <= should account for this scenario anyway. Maybe I'm wrong, it's been a while since I've used PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.