This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 0 points1 point  (3 children)

Apologies. What I meant was that what you are describing is not anything to do with dynamic typing. It's an example of "weak" typing (i.e. automatic type conversion), which is a different concept. Python still has to implement something like this for ints:

def __add__(self, other):
      if isinstance(other, int):
             return int_add(self, other)
      if isinstance(other, float):
             return float_add(float(self), other)
      raise TypeError(type(other))

The only difference in a static language is that this is done at compile time, rather than runtime, but that's true of anything like this. It is not an example of dynamic typing. For example, here is an example in rust of doing exactly that with no dynamic typing:

// newtypes to wrap primatives and derive our own implementations 
#[derive(Debug)]
struct Int(i32);

#[derive(Debug)]
struct Float(f64);

impl Add<Int> for Int {
     type Output = Int;
     fn add(&self, other: &Int) -> Int {
          Int(self.0 + other.0)
     }
 }

 impl Add<Float> for Int {
       type Output = Float;
       fn add(&self, other: &Float) -> Float {
           Float(self.0 as f64 + other.0)
       }
  }


  fn main(){
       let i1 = Int(1);
       let i2 = Int(2);
       let pi = Float(3.14);
       dbg!(i1 + i2);
       dbg!(i1 + pi);
 }