Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics - Milestone #2 #87

Open
wants to merge 112 commits into
base: 2022
Choose a base branch
from

Conversation

abhishekc-sharma
Copy link
Contributor

Milestone 2 for Generics mainly focuses on working correctly with some of the other features.

NOTE: This PR also includes changes by the inheritance team from #83. We probably want to review and merge that one first.

What should work

Generic Functions

It is now possible to create independent functions (not within a class) that are generic. This requires that one of the parameter types is a type-variable. This then allows the return type and variables in the body to also be of that type-variable. Similar to classes, these type-variables are completely unconstrained when the function is type-checked. The instantiations of these types are inferred at the call site and the function is correspondingly monomorphized.

What probably won't work

  • Nested generic functions
  • Combining generic methods and functions ( it's not possible for a method in a generic class to use a type-variable that the class is not generic on)

First-Class Functions

We can now use First-Class Functions in the form of Callable types within generic classes and functions. This allows generic classes to have Callables that use type-variables as fields and parameters to functions.

What probably won't work

  • Using mklambda inside a generic function/method. This doesn't for the sole reason that it would require traversing the entire AST in the type-checker which we didn't think was worth the additional code since this change is already somewhat substantial. We didn't need to do this earlier because type-annotations could never occur in arbitrary expressions. mklambda changes this.

Generic Inheritance

It is now possible to inherit from a class that is also generic. It is also possible to inherit from a specialization of a generic class without the subclass also being generic. Accessing the fields/methods of superclasses should be properly specialized and checked. The subtyping relationship has also now accounts for generic superclasses.

This let's us do pretty cool things like define a generic iterator library with combinators like map (but only to same type) and filter:

T = TypeVar('T')
class Iterator(Generic[T]):
    def hasnext(self: Iterator[T]) -> bool
    def next(self: Iterator[T]) -> T
    def reset(self: Iterator[T])

    def map(self: Iterator[T], f: Callable[[T], T]) -> MapIterator[T]
    def filter(self: Iterator[T], f: Callable[[T], bool]) -> FilterIterator[T]

class MapIterator(Generic[T], Iterator[T]):
    iter: Iterator[T]
    f: Callable[[T], T]
    # Iterator impl that maps f over each element of iter
   
class FilterIterator(Generic[T], Iterator[T]):
    iter: Iterator[T]
    f: Callable[[T], bool]
    # Iterator impl that filters elements of iter with f

class Range(Interator[T]):
    # iterator over a range

r : Range = Range(0, 5)
it : Iterator = r.map(mklambda(...)).filter(mklambda(...))
for i in it:
    print(i) 

What probably won't work

  • Type-parameters to generic superclasses are restricted to int, bool, non-generic class types and type-variables. This limitation is because of the design choice to parse the superclass type-parameters as strings instead of full-fledged types.

What we couldn't do

Explicit Type Annotations

We couldn't add explicit type-annotations due to the delay in merging destructuring assignment and the lack of clarity of the AST representation for expressions separated by commas.

abhishekc-sharma and others added 30 commits May 30, 2022 14:56
Week 8 design & conflicts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants