Skip to content

Instantly share code, notes, and snippets.

@wikii122
Created January 14, 2019 10:17
Show Gist options
  • Save wikii122/c2b470a0fde502df1d1c94aeb7996057 to your computer and use it in GitHub Desktop.
Save wikii122/c2b470a0fde502df1d1c94aeb7996057 to your computer and use it in GitHub Desktop.
# Simple additional info in type showcase
# Assume normal list
class List<T>:
... # List stuff
# Only defining one more type
class Index(int):
# Basically, it's int subtype that on every operation should return int, no overrides
pass
# This Index class means that for instance list searching may return some additional information, like
def find<T>(value: T) -> self.length:
... # impl
# And now, we get elements by integer as always
def __getitem__<T>(idx: int) -> T | None:
... # impl
# But also use this information to validate access
def __getitem__<T>(idx: this.Index) -> T:
...
# Now, this works only when addition saveguards are in place or **when List is immutable**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment