Skip to content

Implement cmp() function #1006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

hikari-no-yume
Copy link
Contributor

@laruence
Copy link
Member

why?

$a < $b? -1 : (($a > $b)? 1 : 0);

you can't change all user used codes into extension function..

@hikari-no-yume
Copy link
Contributor Author

See my last three emails to internals (replying to Rasmus).

Essentially:

  1. Users are bad at writing sort functions:

    function sort_func($a, $b) {
        return $a >= $b; // all too common
    }
    
  2. This is already internal functionality, why not expose it?

  3. It complements strcmp() well.

  4. This means you could unify sorting and user sorting (make cmp() default comparison function).

  5. It's a common use case. This would make writing sort functions easier, especially those that sort multiple things, and it's useful in other places.

  6. Some other languages have an operator for this (notably Perl which has <=>)

@smalyshev
Copy link
Contributor

Why not <=> then? Also, I'm a bit afraid taking "cmp" may be a BC problem. But maybe I'm too paranoid. I like the idea though.

@smalyshev smalyshev added the RFC label Jan 19, 2015
@hikari-no-yume
Copy link
Contributor Author

@smalyshev Yeah, we could just add the operator Perl has, actually. It reads pretty well. I'm sure some would object to the syntax, though, but function ($a, $b) { return $a <=> $b; } makes some sort of sense.

cmp() might cause conflicts, it's true. We actually have a sample function with that name on the usort manual page that does exactly what this built-in would do.

Maybe this'll end up just like the intdiv() RFC proposing a new operator, with a fallback option of a new function should the operator not get in.

@hikari-no-yume
Copy link
Contributor Author

I suppose an advantage of cmp() is it can be easily polyfilled, but <=> does have a certain appeal...

@hikari-no-yume
Copy link
Contributor Author

<=> would avoid the BC issue you're worrying about ^^

@hikari-no-yume
Copy link
Contributor Author

Here's an argument against cmp(): in Perl, $foo cmp $bar is used for string comparison. I doubt familiarity to Perl programmers is an issue here, and people would almost certainly realise that it's not the same given we have strcmp() for that purpose, but it's worth noting.

@laruence
Copy link
Member

@TazeTSchnitzel I've got a idea to fix that issue... running test, thanks

@hikari-no-yume
Copy link
Contributor Author

@laruence Good, but it doesn't really remove the need for this.

@laruence
Copy link
Member

@TazeTSchnitzel actually, I like @smalyshev 's <=> idea

@hikari-no-yume
Copy link
Contributor Author

@laruence I do too, I'm also making a patch for that

@laruence
Copy link
Member

@TazeTSchnitzel as I said, we are not baby-sitter, you can not change any user common used PHP codes into PHP internal function...

@hikari-no-yume
Copy link
Contributor Author

This isn't baby-sitting, it's a basic language feature... Ruby, Perl, Groovy, Python 2 (but not 3?!), Haskell, OCaml, all of these have this.

@laruence
Copy link
Member

@TazeTSchnitzel I was talking about cmp, not <=> :)

@hikari-no-yume
Copy link
Contributor Author

@laruence They're exactly the same, though. If there's a case for an operator, I don't see why there isn't a case for a function.

@hikari-no-yume
Copy link
Contributor Author

Ruby, Perl, Groovy have <=> (Perl also has cmp, the string comparison operator)
Python 2 has cmp()
Haskell and OCaml have compare()

@laruence
Copy link
Member

I feel it is overhead, how different between function ($a, $b) { return $a <=> $b; } and a builtin cmp?

@hikari-no-yume
Copy link
Contributor Author

Nothing, but that's assuming we have <=>. if that doesn't get in, surely we should have cmp().

@hikari-no-yume
Copy link
Contributor Author

Made a <=> patch: #1007

@smalyshev
Copy link
Contributor

Wow unit tests hate cmp()... so maybe operator is better. But RFC can propose both. Actually, even better to take over existing RFC if Davey Shafik doesn't mind (see link in other patch).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants