# How to overload \_\_mul\_\_ (dunder method) in jitclass?

**URL:** <https://numba.discourse.group/t/how-to-overload-mul-dunder-method-in-jitclass/2144>\
**Category:** Community Support\
**Created:** [September 21, 2023, 10:19am UTC](https://numba.discourse.group/t/how-to-overload-mul-dunder-method-in-jitclass/2144 "2023-09-21T10:19:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iperov](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/iperov/32/786_2.png) [@iperov](https://numba.discourse.group/u/iperov)\
**Post date:** [September 21, 2023, 10:19am UTC](https://numba.discourse.group/t/how-to-overload-mul-dunder-method-in-jitclass/2144/1 "2023-09-21T10:19:06Z")

</div>

I want to check argument type in **mul** and depending on the type of argument at compile time, substitute the necessary function for jit

the code does not work:

```auto
import numba as nb
import numba.experimental as nbexp
import numba.extending as nbex

@nbexp.jitclass
class Test:
    def __init__ (self):
        ...
        
    def __mul__ (self, other): 
        ...

@nbex.overload_classmethod(Test, " __mul__")
def test_mul(self, other):
    print('WE ARE HERE')
        
@nb.njit(nogil=True)
def run_test():
    return Test() * Test()

print( run_test() )

```

---

<div class="post-metadata">

**Author:** ![iperov](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/iperov/32/786_2.png) [@iperov](https://numba.discourse.group/u/iperov)\
**Post date:** [September 21, 2023, 2:38pm UTC](https://numba.discourse.group/t/how-to-overload-mul-dunder-method-in-jitclass/2144/2 "2023-09-21T14:38:51Z")

</div>

looks like overload of any method of jitclass also not working

```auto
from numba import types as nbt
import numba as nb
import numba.experimental as nbexp
import numba.extending as nbex

@nbexp.jitclass
class Test:
    def __init__ (self):
        ...
    def some(self, other) -> None:
        ...
    
def Test_some_impl1(self, other):
    return other

@nbex.overload_method(nbt.misc.ClassInstanceType, "some")
def over_some(self, other):
    if self is Test.class_type.instance_type:
        if other in nbt.number_domain:
            return Test_some_impl1

@nb.njit(nogil=True)
def run_test():
    return Test().some(2)

print( run_test() )

```

error:

```auto
AssertionError: Failed in nopython mode pipeline (step: native lowering)
('i64', 'i8*')

```

---

<div class="post-metadata">

**Author:** ![iperov](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/iperov/32/786_2.png) [@iperov](https://numba.discourse.group/u/iperov)\
**Post date:** [September 21, 2023, 3:17pm UTC](https://numba.discourse.group/t/how-to-overload-mul-dunder-method-in-jitclass/2144/3 "2023-09-21T15:17:52Z")

</div>

finally got it working

```auto
import numba as nb
import numba.experimental as nbexp
import numba.extending as nbex
from numba import types as nbt

@nbexp.jitclass([ ('_x', nbt.float32),
                  ('_y', nbt.float32), ])
class Vec2:
    def __init__ (self, x : float, y : float):
        self._x = x
        self._y = y

    @property
    def x(self) -> float: return self._x
    @property
    def y(self) -> float: return self._y

    def __mul__ (self, other): return Vec2(0,0) # overloaded

# Overload implementations
def Vec2 __mul__ Vec2(self, other): return Vec2(self._x*other._x, self._y*other._y)
def Vec2 __mul__ number(self, other): return Vec2(self._x*float(other), self._y*float(other))

# Overloaders
@nbex.overload_method(nbt.misc.ClassInstanceType, " __mul__")
def over_Vec2 __mul__ (self, other):
    if self is Vec2.class_type.instance_type:
        if other is Vec2.class_type.instance_type: return Vec2 __mul__ Vec2
        if other in nbt.number_domain: return Vec2 __mul__ number

# Tests

@nb.njit(nogil=True)
def run_test1():
    return Vec2(1,1) * 2

@nb.njit(nogil=True)
def run_test2():
    return Vec2(1,1) * Vec2(3,3)

print( run_test1().x ) # outputs 2.0
print( run_test2().x ) # outputs 3.0

```

can it be simplified?
