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

Support for generics. #3020

Open
allComputableThings opened this issue Jun 7, 2018 · 0 comments
Open

Support for generics. #3020

allComputableThings opened this issue Jun 7, 2018 · 0 comments

Comments

@allComputableThings
Copy link

Feature request

Currently, I don't think its possible to have a template jitclass. For example, in the

https://github.com/numba/numba/blob/master/examples/stack.py

... the Stack class can only be instantiated to a single element type (in the example, an int).

It would be nice to be able to re-use the stack class for multiple element types:

Something like this:

  IntStackClass = Stack(elemtype=int) # A template class is function (type params ...)->class
  IntStackClass = Stack.instantiate(elemtype=int)
  IntStackClass = numba.instantiate_types(StackTemplate, elemtype=int )
  ...
  IntStackClass().push(1)

We can already do this by wrapping the creation of the stack class in a function who's parameter is elemtype. However, this isn't particularly nice since:

  • it makes the Stack class undocumented when its defined within a function
  • is rather inefficient since we're creating a Stack class object over and over for each call and additional memoization is required to prevent this,

Perhaps:

  @jitclass_template(
      template_type=("elemtype"),
      spec=( ("head": deferred_type()),
                  ("size": numba.intp),
  )
  class Stack(object)
      def __init__():
          self.head = None
          self.size=0

     def push():   
          if self.head is None:
                 self.head = LinkedNode(Stack.elemtype)()  # Template instantiation reduce to LinkedNode#<elemtype>()

          ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants