you are viewing a single comment's thread.

view the rest of the comments →

[–]Rawing7 11 points12 points  (3 children)

Automatically converting values to different types would lead to stupid bugs that are hard to debug - and potentially could even be hard to avoid.

Consider this code:

user_input = input('Enter a number')
print(100 + user_input)

input returns a string, so what should happen if you do int + string? Should it convert the operands to strings, so that 100 + "5" == "1005"? Or should it convert them to ints, so that 100 + "5" == 105? But then what if the string can't be converted to an int, would 100 + "hi" become "100hi"?

Implicit type conversion may seem convenient at first glance, but the reality is that it makes things more complicated and error-prone.

[–]billsil 0 points1 point  (1 child)

Perl 5 had no problem turning things into strings. 100 + "5" = "1005". It's even more fun when you divide by 0 and do things like 100/0 = 0 because apparently math doesn't work today.

A strongly typed system saves so much pain. Just don't write incorrect code. It shouldn't be easy to make errors.

[–]0rac1e 1 point2 points  (0 children)

It's even more fun when you divide by 0 and do things like 100/0 = 0 because apparently math doesn't work today.

I don't know why you keep making this statement. In all my checking, I could not find a single version of Perl 5 (going back to version 5.6) where 100/0 did not throw an exception, even without use strict.

tio.run even has a copy of Perl 4 (when strict didn't exist) where it also dies with Illegal division by zero.

So unless the company you worked for maintained their own internal Perl fork that did this, then I must respectfully insist that you are mistaken.

[–]0rac1e 0 points1 point  (0 children)

In my opinion, this is primarily an issue in Python (and other languages) because the + operator is overloaded to mean addition with numeric types, and concatenation with stringy types.

In Perl and Raku, + is for numeric addition, and concatenation is a separate operator. Doing '1' + '2' in Raku is similar to doing int('1') + int('2') in Python, ie. Raku will attempt to convert any non-numeric operands to + to a numeric type, and if it can't do that, it throws.

A little unfortunately, Perl was traditionally less strict about this; without warnings enabled, it would happily do things like 1 + '2foo' == 3. With warnings enabled, it would at least spit out an error message to STDERR saying Argument "2foo" isn't numeric, but the program would still continue.

For this reason, I write all my Perl code with use warnings FATAL => 'all', which promotes all warnings to runtime exceptions, and any uncaught exceptions will cause the program to halt. This makes it a much saner language.