Hacker News new | past | comments | ask | show | jobs | submit login
The Power of Subscripts in Swift (swiftbysundell.com)
30 points by ingve 58 days ago | hide | past | web | favorite | 6 comments



The article claims that adding subscript support to your own types is unique to Swift, but I immediately thought of how you would do it in Python:

https://docs.python.org/3/reference/datamodel.html#object.__...

I admit I don’t have a ton of experience in other languages. Is this an uncommon language feature?


I wouldn’t say it’s common, but it’s not exactly rare (to reasonably say “unique”) either.

At the end of the day it’s just a form of operator overloading, and is generally present in languages that value expressiveness. Python and C++ (as mentioned), as well as Ruby, Rust, Kotlin from the top of my head. There are many more.

By the way, slightly off-topic—Swift does have a fairly unique feature regarding operators: you can create entirely new ones, not just overloading those existing for some other types.


That unique feature is available to any language that support symbols as names.

All Lisp and ML derived languages, for example.


Subscripts are pretty common. This article is basically a Swift tutorial, I'm not sure what was the point of submitting it to HN.


Maybe in languages that don't have powerful type systems.

I know c++ allows you to overload both the method operator and subscript operators.


All programming languages that support operator overloading offer this as feature.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: