Suppose I want to specialize a generic procedure. Is there any way I can do 
this in nim right now? Example:
    
    
    type
      Vector[N: static[int]] = array[1..N, float32]
    
    proc `<`*[N: static[int], T](a: array[1..N, T]): Vector[N] =
      for i in 1..N:
        result[i] = a[i].float32
    
    proc foo*[N](a: Vector[N]): void =
      echo "foo generic"
    
    proc foo*(a: Vector[3]): void =
      echo "foo 3"
    
    let v: Vector[3] = [1, 2, 3]
    
    foo(v)
    
    
    Run

output: "foo generic"

A particular example where this is helpful is for defining operators over that 
Vector type. I would rather define:
    
    
    proc `+`*[N](a: Vector[N], b: Vector[N]): Vector[N] =
        result = 0
        for i in 0..N:
          result += a[i] * b[i]
    proc `+`*(a: Vector[3], b: Vector[3]): Vector[3] =
        <[a[0] + b[0], a[1] + b[1], a[2] + b[2]]
    
    
    Run

than:
    
    
    proc `+`*[N](a: Vector[N], b: Vector[N]): Vector[N] =
      when N == 3:
        <[a.x + b.x, a.y + b.y, a.z + b.z]
      else:
        result = 0
        for i in 0..N:
          result += a[i] + b[i]
    
    
    Run

to make a faster version of the + operator for vector3s. (In this example the 
speed difference may be negligible, but imagine if I'm summing a lot of 
Vector3s, or if the operator and type were more complicated, like Matrices).

Am I missing a corner case of the language spec, or is this not possible right 
now? 

Reply via email to