I believe that this does actually qualify as duck typing. The saying "if it 
walks like a duck and quacks like a duck, then it's a duck" refers to a form of 
generic or type agnostic code where a type is usable based on its 
functionality. Aka, the types `int` and `float` have a `+=(_: int)` and as such 
they are valid for use in the `proc` with `auto`.

Structural typing means that types are interchangeable if their definitions are 
equal. In Nim this happens with tuples. If tuples have the same elements (by 
number and type), they are synonyms and interchangeable.

The above is an example of duck typing.

Here's some structural typing: 
    
    
    type
      Vec = tuple [x, y, z: float]
      Color = tuple [r, g, b: float]
    
    proc interpolate(vecOrColor: Vec; weight: float): Vec =
      # mix color or vector values
    
    let a = Vec(1.0, 1.0, 1.0)
    interpolate(a) # works
    
    let b = Color(r: 1.0, g: 2.0, b: 3.0)
    interpolate(b) # also works
    

(Hopefully I have the correct syntax there, I'm not somewhere with the 
compiler) 

Reply via email to