top | item 36532548

(no title)

Dessesaf | 2 years ago

Your `f` function is just polymorphic.

Would you also consider the following Haskell function to not be strongly typed?

    add a b = a + b
This is a polymorphic function that works for any type that has a Num instance (has functions `+`, `-`, `*`, etc.). Just like the python version works for any type that implements __add__().

It's just that in Haskell's case, this checking is done at compile-time and in python's case at runtime.

discuss

order

tgv|2 years ago

But, to push it a bit further: how does Haskell deal with [1, 2] + ["a", {"x": 1}]? I think it can only do that if it knows the type of both. In Python, you don't declare those types. It's just an array. The result is just another array. It's as if the arguments to the operator get cast to [Any], where Any is the union of all possible types, which is then also the result type.

Or, if you want to look at it in another way: every function, class member and variable is super-hyper-extra-polymorphic, with the types limited by one or two sanity checks at runtime for a handful of operations that happen to apply to them. If my f() would avoid the addition operator sometimes, e.g. like this:

    def f(x, y):
        if somepredicate(x):
            return y
        return x + y
then y can be anything when somepredicate(x) is true, and the type of f might not even be computable. The type of f certainly would never be verified by Python beyond "is it a function of two arguments".

It may not be weak, but I'm not sure calling it "strong typing" makes sense.

dunefox|2 years ago

It is strongly typed as 1 + "1" doesn't work. This has nothing to do with knowing the types beforehand and disallowing the execution.