3 ms·
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
by williamjackson 7y ago
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.__getitem__ 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?
- Something1234 7y agoMaybe in languages that don't have powerful type systems. I know c++ allows you to overload both the method operator and subscript operators.
- krzat 7y agoSubscripts are pretty common. This article is basically a Swift tutorial, I'm not sure what was the point of submitting it to HN.
- uranusjr 7y agoI 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.
- pjmlp 7y agoThat unique feature is available to any language that support symbols as names. All Lisp and ML derived languages, for example.
- pjmlp 7y agoAll programming languages that support operator overloading offer this as feature.