Created
January 14, 2019 10:17
-
-
Save wikii122/c2b470a0fde502df1d1c94aeb7996057 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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