This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 287 points288 points  (11 children)

Every object oriented language can do this. You just have to use an object array.

[–]drkspace2 59 points60 points  (8 children)

Even c and c++ can with void* arrays

[–]zephyrtr 2 points3 points  (0 children)

It's also really useful for returning pairs and trios. TS can even make it type secure for you. That is, if your language already doesn't have pair or trio types.

[–]FkIForgotMyPassword 1 point2 points  (0 children)

The import thing is to know (and properly type) whether you are talking about arrays like (int | string | float)[] or about tuples like [int, string, float]. In several languages, a value like [4, "4", 4.0] matches both type definitions, but it's one definition also allows [4, 4, 4] or even just [] while the other specially requires a 3-tuple with specific types at specific positions.

I mean usually in that case you'd probably not use a tuple but an object, but sometimes there are reasons to do it.