2

Does anyone understand the reasoning behind making case-sensitive and case-insensitive versions of each of PowerShell's comparison operators? And why make the default case-insensitive?

For example, take -eq and -ceq. These operators are used to compare all types, not just Strings. So what does 0 -ceq 1 even mean then? How can you case-insensitive compare two integers?

Dan
  • 1,215
  • 1
  • 10
  • 22
  • 5
    Because that – case insensitivity – is often what is wanted when performing administrative scripting (especially from the command point. But this question is Not Constructive for SO (no objective answer possible unless you are lucky enough to get one of the PSH team like [here](http://stackoverflow.com/a/573861/67392)). – Richard Jun 20 '12 at 17:32

1 Answers1

3

Rather obviously, because sometimes you want case sensitivity, and sometimes you don't. Case insensitive is a reasonable default, because when you are doing ad-hoc queries from the command line, you don't want to have to do extra work to ensure you see the most common examples of what you're trying to match.

As for what 0 -ceq 1 might mean: who cares? Really! Just don't code that if you don't want to find out.

Dominic Cronin
  • 6,062
  • 2
  • 23
  • 56
  • 3
    I think everyone should care. The equal operator, no matter what the language, should serve all objects equally. But PowerShell's implementation is a huge departure from that, and inconsistent with the .NET framework (and languages) to which it promotes its tight coupling. The power of PowerShell is in how naturally you can interact with any .NET object, not just Strings. I say create other, dedicated operators for doing String comparisons, and save the -eq operator for equality comparisons. – Dan Jun 21 '12 at 05:45
  • 1
    OK - flippancy aside. If `0 -eq 1` and `0 -ceq 1` don't mean pretty much the same thing, then it would obviously be broken. Powershell places high value on consistent and general approaches... e.g. everyone uses the same verbs. I can see that this would apply equally to operators. You want a standard set of operators that you can use with anything. Yes - of course, they should work "correctly", but I'd still advise people to stay away from poorly-understood edge cases. (Have you tried `1 -eq "1"` ?) – Dominic Cronin Jun 21 '12 at 08:09