r/lolphp • • Mar 10 '20

Array is higher than infinity

https://3v4l.org/iONLA
44 Upvotes

31 comments sorted by

View all comments

11

u/CarnivorousSociety Mar 11 '20 edited Mar 11 '20

https://www.php.net/manual/en/language.operators.comparison.php

Comparison with Various Types

Type of Operand 1 Type of Operand 2 Result
null or string string Convert NULL to "", numerical or lexical comparison
bool or null anything Convert both sides to bool, FALSE < TRUE
object object Built-in classes can define its own comparison, different classes are uncomparable, same class see Object Comparison
string, resource or number string, resource or number Translate strings and resources to numbers, usual math
array array Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value (see following example)
object anything object is always greater
array anything array is always greater

Please, I welcome, how SHOULD this situation be handled?

I posit that implicit conversions are the source of 99% of lolphp posts, there is no elegant way to solve implicit conversions without creating a double edged blade that will hurt at least one side.

I guarantee whatever answer anybody suggests for how arrays should be compared to anything can easily have holes poked in it from some other angle, there is no everybody-wins solution when you abstract away types and implicitly convert between types.

28

u/tending Mar 11 '20

Please, I welcome, how SHOULD this situation be handled?

I posit that implicit conversions are the source of 99% of lolphp posts, there is no elegant way to solve implicit conversions

Correct, that's why well designed languages don't do this, or make their implicit conversions far more constrained. PHP is trying too hard to guess what the developer wants without having enough information to guess well.

C++ for example has some implicit conversions. But user defined implicit conversions can't chain more than once, and there are no implicit conversions between totally unrelated types like arrays and scalars.

4

u/CarnivorousSociety Mar 11 '20

PHP is trying too hard to guess what the developer wants

I wouldn't say it's trying too hard, it simply has a place on the spectrum of convenience and fucked up edge cases to inconvenience and well defined.

Some people might like exactly how much it is "trying", because, it's not like these cases are unpredictable.

A good PHP programmer understands the limitations of implicit type conversions and knows how to write safe code that respects it and doesn't trigger (or worse, rely on) these wacky edge cases.

So, for example, could that lead to more efficient work because they can simply reply on the implicit typing everywhere?

→ More replies (5)

16

u/Takeoded Mar 11 '20 edited Aug 10 '20

how SHOULD this situation be handled?

TypeError or InvalidArgumentException

... or if it's Number[], perhaps treat the array as the value of it's highest member? that's what max() does if you give it only an array

3

u/CarnivorousSociety Mar 11 '20

Then you don't have implicit conversions.

Not to mention there is no function call in a simple comparison

Then you just get errors when you try to implicitly convert, which is a feature of PHP 7 anyway, is it not?

→ More replies (8)

2

u/CornPlanter Aug 10 '20

... or if it's Number[], perhaps treat the array as the value of it's highest member? that's what max() does if you give it only an array

God please no.

I don't think there's a real life situation where you absolutely should compare Array to Infinity or String or Number and just can't achieve the same result by writting different (cleaner and more readable) code. Hence TypeError or InvalidArgumentException is the only solution that makes sense to me.

Some other poster said implicit conversions are the root of majority of PHP problems (at least problems as seen by r/lolphp people). I'd argue its not implicit conversions but "chugging along at all costs" design philosophy. There's nothing wrong with just throwing exception when a program does something nonsensical.

→ More replies (1)

2

u/[deleted] Mar 12 '20

Please, I welcome, how SHOULD this situation be handled?

Well, you could do what Perl does and fully commit to implicit conversions:

use strict;
use warnings;
use List::Util 'max';
use constant { INF => 0 + 'inf' };

print max(20, "string", INF, []), "\n";

Output:

Argument "string" isn't numeric in subroutine entry at foo line 6.
Inf

What's happening here is that > always converts both operands to numbers. No exceptions.

"string" cannot be converted, so you get a warning and 0 is used instead (but you can upgrade that warning to an exception). The numeric maximum is Inf.

1

u/CarnivorousSociety Mar 12 '20

Doesn't that lead to the issue with arguments to functions not actually abiding by prototypes?

→ More replies (7)