r/nim•
Ruri

Newbie Question about Proc Declaration

Got an (I think) fairly simple question that should have a fairly simple answer, but I'm having a surprisingly hard time finding answers in the documentation.

Take this simple example of a procedure from Nim by Example:

proc zip[I, T](a, b: array[I, T]): array[I, tuple[a, b: T]] = 
    for i in low(a)..high(a): 
        result[i] = (a[i], b[i]) 

let nameAndAddresses = names.zip(addresses)

For reference, this example is included with an explanation of arrays, showing that a dynamic array's size must be passed to a procedure with its value(s).

All I want to know is what that [I, T] after the procedure name (zip) is. I suspect it indicates something about the return type (which is another array of variable size like it is being passed) but I just want to make sure I understand exactly what that means because I keep seeing it in other places and it's always a big question mark for me.

Any guidance?

Posted Aug 2, 2022, 2:52 AM UTC

146
Beef331•

It's a generic procedure that operates on any `array`. Nim's `array` are not dynamic array, they're static sized. `seq` is Nim's dynamic array. the `[I, T]` are generic parameters which bind to the `a` parameter's `[I, T]`

5
RuriOP•

Thanks for the reply! I had to research a bit what "generic types" are in Nim but now I think I understand.

3
Patitotective•

It creates a generic procedure, meaning that a new zip procedure will be instantiated whenever you pass a different array of size or type.Take a look at this https://nim-lang.org/docs/manual.html#generics.

1
RuriOP•

This is helpful. So this allows one to pass a static array of variable sizes and constituent types and handle all the different kinds of types one might receive in a "generic" way. Am I understanding this correctly?

1
Patitotective•

Yep, just like u/Beef331 said this generic can handle any Nim array.

1
RuriOP•

Appreciated. Thanks!

1