r/programminghorror • • Jun 12 '25 • This 1

Javascript Javascript is filled with horror

Post image
2.4k Upvotes

337 comments sorted by

View all comments

Show parent comment

1

u/No_Pen_3825 Jun 18 '25

Yeah our functions are actually variables too. func foo() is var: foo () -> Void. My point is not that JS can’t do what Swift can; it’s unfair to ask a bird to swim then blame it when it gets outdone by a fish even though they’re both animals. My point is the statement “they’re exactly the same” is outright false.

1

u/LordFokas Jun 18 '25

ok I might have gone a little bit overboard with "exactly" 😅 Sorry about that.
Allow me to correct now that I'm better informed:

The extensions in both languages work almost the same way.
Due to the type system, we don't have some of the more fine grained stuff, but the rest is more or less there -- the things that don't work the same way, can still be implemented in a way to achieve the same behavior.

And all this to say that if you're lacking a helper method in JS you can still add it yourself the same way you can in Swift.

Again, due to the fine grained type stuff being lost, a method you'd make specifically for arrays of oranges, in JS, has to be added for all kinds of arrays. If you need to be strict about that you may want to peek into the array contents, but you're not blocked from implementing your thing, which would be worse IMO.

I have such a method, that only makes sense (and only works) for Promises of Arrays of things. For all other Promises it doesn't make sense and I never call it, it's just there, in its corner. Calling it would likely straight up crash the program. But it only makes sense in the same contexts where it is safe, so I don't even get confused or anything.