News for hackers. Stuff that matters.
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by azangru on Wednesday April 12, 2023 @08:25AM (#35538533)
Well, we are still in the "play silly games" territory, aren't we? What should comparing a string to a number equal anyway?
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by tialaramex on Wednesday April 12, 2023 @08:34AM (#35538638)
The program is semantically incorrect, the compiler or interpreter should complain that you are attempting to ask for equality of a string and a number, and it doesn't know how to answer such a question. Then the programmer can fix the mistake.

Here's Rust for example: error[E0277]: can't compare `&str` with `{integer}`

[The full diagnostic points to where exactly your code tries to perform such a comparison and explains that you could do this if PartialEq<{integer}> was implemented for &str, which it is not, and then suggests things which are PartialEq for &str, such as other strings and various string-like objects a sane person might compare them to]

Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by Someone on Wednesday April 12, 2023 @08:32AM (#35538614)
> What should comparing a string to a number equal anyway?

I think there are two valid options: false and signaling an error (by any means such as returning null, throwing, etc). JavaScript chooses the third (for some strings)

Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by msla on Wednesday April 12, 2023 @08:28AM (#35538563)
What should 0x40 == 64 equal? Ought hexadecimal integer and decimal integer be the same type?

As long as conversion between representations is well-defined and not hugely surprising, what does it matter?

Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by aflag on Wednesday April 12, 2023 @08:32AM (#35538616)
They are the same type and represent the same number. Just different literals. "ABC" is also the same as "A" + "B" + "C".
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by tialaramex on Wednesday April 12, 2023 @08:51AM (#35538821)
> As long as conversion between representations is well-defined and not hugely surprising, what does it matter?

Surely the MD5 example demonstrates exactly how this ends up being a problem.

On the surface "1e5" == 100000 seems like a reasonable conversion between representations and unsurprising, but by silently converting things and then comparing the result we get this astonishing result.

Conversions need to be made explicit. This is an opportunity for programmers to consider whether they actually wanted a conversion (the MD5 programmer did not) and for API designers to offer multiple distinct conversions when appropriate. "1e5" == 100000 is only one reasonable interpretation. Maybe it's a lowercase hexadecimal value. If you make the programmer explicitly convert they can choose.

Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by giantrobot on Wednesday April 12, 2023 @11:03AM (#35540950)
> On the surface "1e5" == 100000

Coercing an explicit string e.g. "1e5" to a numeric is boneheaded. It's fine to support different numeric formats like exponent or hex format. Weak typing and type coercion is a major footgun in a language.

Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by msla on Thursday April 13, 2023 @05:03AM (#35552814)
> Weak typing and type coercion is a major footgun in a language.

Type coercion is an expression of strong typing, and it's inevitable if you want a language that's usable.

For example, "array of int of length 5" is a type, and "array of int of length 6" is a different type. Do you want to write a separate sort function for each of those types?

Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by tialaramex on Friday April 14, 2023 @06:34AM (#35567819)
I don't want to write a separate sort function, the computer can and should do all that work for me automatically.

Because Default is tricky to genericise, there literally are 32 distinct implementations provided for Rust's [T; 1] through [T; 32] of Default::default()

https://doc.rust-lang.org/src/core/array/mod.rs.html#467

Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by pdpi on Wednesday April 12, 2023 @09:23AM (#35539232)
> As long as conversion between representations is well-defined and not hugely surprising, what does it matter?

In Javascript:

    let a = '0';
    a == !a; // True. Why?
    let b = !a;
    (a == b) == (!a == !b) // False. Why?
None of the coercions involved is particularly surprising, but the result most definitely is.
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by flir on Wednesday April 12, 2023 @09:13AM (#35539095)
It should throw FileNotFound.
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by wizzwizz4 on Wednesday April 12, 2023 @10:29AM (#35540317)
For those who missed the reference: https://thedailywtf.com/articles/What_Is_Truth_0x3f_
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by michaelt on Wednesday April 12, 2023 @10:26AM (#35540259)
I'm not sure about comparing strings to numbers.

But I know I appreciate it when I can query my database with:

  SELECT * FROM orders WHERE delivery_day
    between '2023-04-10' and '2023-04-12'
Instead of using

  SELECT * FROM orders WHERE delivery_day 
    between PARSE_DATE('%Y-%m-%d', '2023-04-10')
    and PARSE_DATE('%Y-%m-%d', '2023-04-12')
So I can see why language designers put in some implicit conversions.
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by jmalicki on Wednesday April 12, 2023 @04:14PM (#35546106)
The difference is that it won't turn "abcjhd" into "1970-01-01", but rather issue an error.
Re:Why MD5('240610708') is equal to MD5('QNKCDZO')? by jerf on Wednesday April 12, 2023 @10:40AM (#35540511)
I'll cop to having cooled a lot on dynamic languages in general over the past 10 years, but broadly speaking I think it's reasonable to say the decades of experience of the various dynamic languages leans in favor of == being strict in terms of the types of the operands and making it either "false" or an exception. The convenience of comparing a string "124" to the number 124 is outweighed by the near-continuous stream of bugs generated by the languages that will happily yield "true" for that. If I were writing a new dynamic language I'd look to emulate Python's == more than Perl or Javascript here.

In general == should not coerce types automatically. Automatic type coercion, even in dynamic languages, is the worst aspect of dynamic typing.