Remix.run Logo
josephg 7 months ago

I think the point is that in newer languages like typescript, the price paid for static typing is lower because type inference does so much of the leg work. You get all the benefits of static typing, and the cost is usually tiny - you just need to define your types (a valuable exercise regardless) and add them to function signatures.

We’ve come a long way from the C++ or Java I wrote when I was young, where types were named and renamed constantly. As I understand it, even C++ has the auto keyword now.

maleldil 7 months ago | parent [-]

C++ has extensive type inference now (C++20):

    #include <string>

    auto func(auto x, auto y) -> auto {
        return x + y;
    }

    auto main() -> int {
        auto i = func(1, 2);
        auto s = func(std::string("a"), std::string("b"));
    }

`int` is required as a return type from `main`, but everything else is inferred. This works because `func` becomes a template function where each parameter type is a separate template type, so you get compile-time duck typing. It also works with concepts (e.g. `std::integral auto x`).

It's quite neat, but I don't think anyone actually writes code this way, except for lambdas.