If you break it down further
func toArray(str: static string): array[str.len(), char] =
var i = 0
for c in str:
result[i] = c
inc i
Run
(This also doesn't work just for illustration) This is a single function, with
the return value set as array[0,char]
Since there's only one instantiation of the function, what else could the
length of the array be?
You need a generic, instantiated based on the length of that string
func toArray[L:static int](str:string):array[L,char]
Run
Would work, even, but your way is clearly better.