Hacker News new | ask | show | jobs
by Jtsummers 1074 days ago
How is OCaml strongly typed if it doesn't have declared types?!?!? (EDIT: In case it's not obvious, I'm being sarcastic, I'm pretty sure some people in this discussion won't get that though.)

  let f x = x + 1;; (* What's the type?!?!? *)
Turns out that "strong typing" is a shitty phrase that people should stop using because it means too many conflicting things, and, consequently, means nothing. Static and dynamic typing have well-defined meanings, stick with those terms instead of ones that mean nothing.

But for a demonstration (compare to Perl) try this in your Python REPL:

  >>> 1 + "1"
Does it work? Probably not unless you futzed with the language implementation. In Perl it does, though. So to the extent that "strong typing" means anything, Perl is "weakly typed", Python is "strongly typed", and both are dynamically typed. It's an orthogonal characteristic of the type system and language from when type checking occurs.

----------

EDIT: BTW, formatting code blocks on HN is really easy. Prefix each line of code with two space characters.

  __Replace those _'s with spaces
The result is much cleaner than your comment:

  def foo(x):
    return x + x
No extra newlines needed, more compact, easier for most people to read.
1 comments

people on HN often claim that Python is "strongly typed" while PHP is loosely-typed, but I don't see the difference honestly. both are pretty loose.

re: 1 + "1"

I didn't get your point really. My reply was to counter claim that Python is supposedly "strongly typed" and I don't understand how this strong typing helps developers. I know that languages can infer types, which is tangential subject. I dont know why you brought this up

> I don't see the difference honestly. both are pretty loose.

There's a clear difference, in PHP 1 + "1" is 2, in Python it's a TypeError, (and as a bonus, in Javascript 1 + "1" is "11").

The definition of "strongly typed" being used is related to type coercion, not type inference. In PHP the string is being coerced to an integer, but Python requires you to explicitly say 1 + int("1") if you want to add the numbers together. This can be helpful to developers because it requires you to make a decision about what behavior you actually want rather than assuming you want to add two numbers or concatenate strings when you may have wanted the opposite.

String concatenation is probably the most reasonable result aside from a type error given how + is used in JS for concatenation elsewhere, but JS actually gets funny.

  "1"*2 // => 2 (not "2")
  "1"*2+3 // => 5
  "1" - 2 // => -1
  "9"/3 // => 3
  "9" + 3 // => "93"
So some mathematical operators will convert string parameters to a number, but not +.
What's the difference between PHP giving "2" as the result of 1 + "1" and Python giving "999" as the result of "9" * 3?
Try that line in both Python and Perl and see how they behave, you'll see that one (Python) respects types (the most useful notion of "strong typing" is no or limited automatic type coercion and "weak typing" as an excessive permissibility around mixing of types in operations) and the other (Perl) does not.

I pointed out the OCaml example because both you and your sibling poster brought up type declarations as somehow mattering with respect to "strong typing". OCaml doesn't require type declarations, so I guess it's weakly typed according to both of you. Which is a surprising result.

I never do 1+”1” in my code, so this example is not useful to me.

I do however annotate types and expect Python to respect type annotations which is not the case. Then I dont understand what is point of annotating types if they are not respected?

If your argument that Python doesn’t convert from one type to another - well, it doesn’t need to do that if doesn’t care about types in the first place and lets you pass any junk into any method (and this is #1 thing that type system is supposed to prevent)

Is it only for documentation so that people reading code could understand what types to pass?

Python type annotations were added to be used by an external type checker, so no they are not enforced by the interpreter itself.

This was an explicit decision: https://peps.python.org/pep-0484/#non-goals

Python type hinting is not useful at runtime (in fact it's flat out ignored). It is useful at "linting" time when run through mypy.