News for hackers. Stuff that matters.
Re:TypeScript is terrible for library developers by 323 on Tuesday August 23, 2022 @03:55PM (#32570571)
Python is also very dynamic, yet libraries rarely allow arguments to be number/string/list/dict/function at the same time.

Yet in JavaScript I see this all the time - pass a string URL, but we also accept a function which will return a string URL, or maybe an option object - { url: string, strict: boolean }

So I think it's more of a culture thing.

Re:TypeScript is terrible for library developers by nerdponx on Tuesday August 23, 2022 @04:11PM (#32570808)
Python libraries kind of used to do this more, but seem to be doing it less now, in part because highly-polymorphic interfaces are a huge ugly pain in the butt with the Python type hinting system.
Re:TypeScript is terrible for library developers by UncleEntity on Tuesday August 23, 2022 @09:43PM (#32574052)
> Python libraries kind of used to do this more, but seem to be doing it less now…

I’d argue that it has to do with C extensions making it a lot of work to get it right.

Some library code does it (which is where I learned how to do it ‘correctly’) but in general, run of the mill code it is pretty rare. Just something like an overloaded __add__ turns into a page and a half of code and there’s a lot of functions in the numbers protocol struct.

Labor of love really.

Re:TypeScript is terrible for library developers by int_19h on Tuesday August 23, 2022 @05:35PM (#32571872)
What's so painful about writing

   foo: int | str
Re:TypeScript is terrible for library developers by nerdponx on Tuesday August 23, 2022 @06:51PM (#32572621)
It gets a lot more complicated than that. One of many, many examples: https://github.com/pandas-dev/pandas-stubs/blob/v1.4.3.22082...

In general, if someone with experience in a domain is complaining that something is hard, it's safe to assume that the thing is actually at least kind of hard, and that they aren't just a crybaby about indentation and formatting.

Re:TypeScript is terrible for library developers by int_19h on Tuesday August 23, 2022 @08:30PM (#32573511)
I don't see what's complicated about this example. It adds overloads, but those aren't hard, either.
Re:TypeScript is terrible for library developers by nerdponx on Tuesday August 23, 2022 @08:56PM (#32573709)
Fair, it's not "hard". But I stand by my assertion that it's a huge ugly pain in the butt. That doesn't mean it's not worth doing, but that doesn't make it less huge, ugly, or painful.

I also assert that if NDFrame.loc were less polymorphic (e.g. if you had a separate accessor like NDFrame.mask for boolean subsetting) it would be easier to use, easier to reason about, easier to teach, easier to document, and easier to write type hints for.

The worst situation is when you want to have multiple return types depending on the argument values, like Pandas' inplace=True. Putting aside the problem of dependent types, even supporting only literal True and False requires you to write out every single overload twice, creating a literal combinatorial explosion of overloads.

And that's also ignoring the whole world of parametric polymorphism required for higher-order functions, factory functions etc.

Maybe the situation is no better in something like C++, but I don't know enough C++ to have an opinion on it.

Re:TypeScript is terrible for library developers by fshr on Wednesday August 24, 2022 @12:16AM (#32574920)
To be fair, there are few things as here-be-dragons as the guts of Pandas. It'd be hard to find a more rarified example, but it served to further the conversation at least.
Re:TypeScript is terrible for library developers by nicoburns on Tuesday August 23, 2022 @03:59PM (#32570633)
Luckily these types are not complex at all to type in Typescript. You just enumerate all the options with a | character separating them.
Re:TypeScript is terrible for library developers by 323 on Tuesday August 23, 2022 @04:03PM (#32570679)
Right, but people compose. They take this union and put it in another object, which also allows multiple types for some field, and then they take that object and put it in another one, and then you have a 10 line type declaration for 3 fields.
Re:TypeScript is terrible for library developers by isbvhodnvemrwvn on Wednesday August 24, 2022 @03:21AM (#32575922)
Which is why you can have aliases for those types, each level of abstraction can be nicely wrapped up. It just describes the reality.
Re:TypeScript is terrible for library developers by eyelidlessness on Tuesday August 23, 2022 @10:03PM (#32574178)
I’m mostly sitting this out, but I’ll interject here because this is wrong. Union types are often used as an XOR (a | b as “either a or b, but not both”), but they’re an inclusive OR (Partial<a> & Partial<b>, as “some parts of a or b, the parts they share guaranteed”) which is almost never what anyone wants when dealing with objects. The pain is that you can’t distinguish them without a narrowing factor, and if you can’t the types are horridly complex.
Re:TypeScript is terrible for library developers by colejohnson66 on Tuesday August 23, 2022 @11:49PM (#32574775)
I mean, that makes sense. If types `A` and `B` both have a function `doStuff`, is it bad to be able to do `x.doStuff()` without narrowing?

For example: every object’s prototype chain ends with `Object.prototype`, so all objects have a `toString` function. I shouldn’t be forced to narrow my `number|bigint|string` (for example) just to do `x.toString()`. If I was, we’d get this ugly and bloated mess:

    function callToString(x: number | bigint | string)
    {
        if (typeof x === "number")
            x.toString();
        else if (typeof x === "bigint")
            x.toString();
        else
            x.toString();
    }
Regardless, they’re just copying JavaScript’s duck typing feature. It’s not much different than languages like Java and C# that have interfaces. In C#, if my parameter is IClonable, I don’t care what it is, but know there’s a `Clone()` method attached to it.